mateodioev/http-router
Composer 安装命令:
composer require mateodioev/http-router
包简介
Simple route in php with support for vars in url
README 文档
README
⚠️ This project was made for educational purposes only. It is not intended to be used in production environments.
Usage
use Mateodioev\HttpRouter\exceptions\{HttpNotFoundException, RequestException}; use Mateodioev\HttpRouter\{Request, Response, Router}; $router = new Router(); // Register your endpoints here $router->get('/', function (Request $r) { return Response::text('Hello world!'); }); $router->run();
Methods
You can use the Router::get, Router::post, Router::put, Router::patch and Router::delete methods to register your endpoints.
$router->myHttpMethod($uri, $callback);
$uriis the path of the endpoint.$callbackis the function that will be executed when the endpoint is requested. Each callback (action) must return an instance of the Mateodioev\HttpRouter\Response class or an InvalidReturnException will be thrown.
Static files
You can map all static files in a directory with the static method.
$router->static($baseUri, $path, $methods); // Default methods are GET
Example:
tree
.
├── index.php
└── styles.css
1 directory, 2 files
$router->static('/docs', 'public/');
Now you can reach this uris
- /docs/index.php
- /docs/styles.css
Handling all HTTP methods
You can use the Router::all method to handle all HTTP methods with one callback.
$router->all($uri, $callback);
Path parameters
You can use path parameters in your endpoints. Path parameters are defined by a bracket followed by the name of the parameter.
/users/{id}
$router->get('/page/{name}', function (Request $r) { return Response::text('Welcome to ' . $r->param('name')); });
Note: You can make a parameter optional by adding a question mark after the name of the parameter.
/users/{id}?
$router->get('/page/{name}?', function (Request $r) { $pageName = $r->param('name') ?? 'home'; // If the parameter is not present, the method return null return Response::text('Welcome to ' . $pageName); });
Request data
You can get all data from the request with the following methods:
Request::method()returns the HTTP method of the request.Request::uri()returns the URI of the request.Request::uri()returns the URL of the request.Request::param($name, $default = null)returns the value of the parameter with the name$nameor null if the parameter is not present.Request::params()return al the request URI parameters.Request::headers()returns an array with all the headers of the request.Request::body()returns the body of the request (from php://input).Request::data()returns an array with all the data of the request. Use this when Content-Type is application/x-www-form-urlencoded or multipart/form-data.Request::files()returns an array with all the files of the request.Request::query()returns an array with all the query parameters from the uri.
TODO list:
- Add support for middlewares.
- Add support for custom error handlers.
mateodioev/http-router 适用场景与选型建议
mateodioev/http-router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 42 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 06 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mateodioev/http-router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mateodioev/http-router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 42
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-06-16