fastd / routing
FastD 路由组件
Requires
- php: >=5.6
- fastd/http: ^3.0
- fastd/middleware: ^1.0
Requires (Dev)
- phpunit/phpunit: ^5.0
- dev-master
- 5.0.x-dev
- 3.3.x-dev
- v3.2.6
- v3.2.5
- v3.2.4
- v3.2.3
- v3.2.2
- v3.2.1
- v3.2.0
- 3.1.x-dev
- v3.1.4
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- 3.0.x-dev
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v3.0.0-rc2
- v3.0.0-rc1
- 2.0.x-dev
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- 2.0.0-rc1
- v2.0.0-beta2
- v2.0.0-beta1
- v0.1.2
- v0.1.1
- v0.1.0
- dev-develop
This package is auto-updated.
Last update: 2024-08-24 15:56:27 UTC
README
简单的PHP路由器,支持路由嵌套、动态路由、模糊路由、中间件等。依赖于http组件。
声明
- PHP 7.2
Composer
Composer require "fastd/routing"
使用
您可以通过RouteCollection
对象设置路由,或者通过路由列表创建路由。详细文档:fastd/routing
静态路由
use FastD\Http\ServerRequest; use FastD\Routing\RouteCollection; $collection = new RouteCollection(); $collection->addRoute('GET', '/', function () { return 'hello world'; }); $route = $collection->match(new ServerRequest('GET', '/')); // \FastD\Routing\Route echo call_user_func_array($route->getCallback(), []);
路由匹配不会调用路由的回调,而是返回整个路由对象供回调处理,因此match
只返回匹配的路由对象。
动态路由
use FastD\Http\ServerRequest; use FastD\Routing\RouteCollection; $collection = new RouteCollection(); $collection->addRoute('GET', '/{name}', function ($name) { return 'hello ' . $name; }); $route = $collection->match(new ServerRequest('GET', '/foo')); // \FastD\Routing\Route echo call_user_func_array($route->getCallback(), $route->getParameters());
在动态路由下,成功匹配的路由会将匹配参数更新到getParameters
,并通过getParameters
获取匹配参数信息。
同一路由,多个方法
$collection = new FastD\Routing\RouteCollection(); $collection->get('/', function () { return 'hello GET'; }); $collection->post('/', function () { return 'hello POST'; }); $response = $collection->dispatch('GET', '/'); // hello GET $response = $collection->dispatch('POST', '/'); // hello POST
混合路由
在许多情况下,我们的路由可能只相差一个参数。以下是一个示例。
use FastD\Http\ServerRequest; use FastD\Routing\RouteCollection; $collection = new RouteCollection(); $collection->addRoute('GET', '/{name}', function () { return 'get1'; }); $collection->addRoute('GET', '/', function () { return 'get2'; }); $route = $collection->match(new ServerRequest('GET', '/abc')); // \FastD\Routing\Route $route2 = $collection->match(new ServerRequest('GET', '/')); // \FastD\Routing\Route echo call_user_func_array($route->getCallback(), $route->getParameters()); echo call_user_func_array($route2->getCallback(), $route2->getParameters());
路由分组
路由分组将为其每个子路由添加自己的路由前缀,支持多级嵌套。
use FastD\Http\ServerRequest; use FastD\Routing\RouteCollection; $collection = new RouteCollection(); $collection->group('/v1', function (RouteCollection $collection) { $collection->addRoute('GET', '/{name}', function () { return 'get1'; }); }); $route = $collection->match(new ServerRequest('GET', '/v1/abc')); echo call_user_func_array($route->getCallback(), $route->getParameters());
模糊路由
模糊路由的灵感来源于Swoole http服务器的onRequest回调。因为每个路由条目都会通过onRequest,当它被创建时,可能根据pathinfo处理一些特殊的路由。然后可以发送模糊路由。现在是使用它的时间。
use FastD\Http\ServerRequest; use FastD\Routing\RouteCollection; $collection = new RouteCollection(); $collection->addRoute('GET', '/api/*', function ($path) { return $path; }); $route = $collection->match(new ServerRequest('GET', '/api/abc')); echo call_user_func_array($route->getCallback(), $route->getParameters()); // /abc $route = $collection->match(new ServerRequest('GET', '/api/cba')); echo call_user_func_array($route->getCallback(), $route->getParameters()); // /cba
匹配所有以/api
开头的合法路由,然后进行回调
路由中间件
路由组件基于[Http] (https://github.com/JanHuang/http) 和 [HTTP Middlewares] (https://github.com/JanHuang/middleware) 实现路由中间件。
路由中间件回调自动以参数调用
Psr\Http\Message\ServerRequestInterface
和FastD\Middleware\DelegateInterface
。
中间件调用完成后,返回\Psr\Http\Message\ResponseInterface
对象,以便程序处理输出。
use FastD\Http\ServerRequest; use FastD\Routing\RouteCollection; $collection = new RouteCollection(); $collection->addRoute('GET', '/api/*', function (ServerRequest $request) { return 'hello'; }); $dispatcher = new \FastD\Routing\RouteDispatcher($collection); $response = $dispatcher->dispatch(new ServerRequest('GET', '/api/abc')); echo $response->getBody();
测试
phpunit
贡献
我很高兴能够参与到一个更好的PHP生态系统的创建中,我是Swoole生态的开发者。
如果你对此感到满意,但不知道如何开始,你可以尝试以下事情
- 系统遇到的问题 [反馈] (https://github.com/JanHuang/fastD/issues)。
- 有更好的建议?请随时联系 [bboyjanhuang@gmail.com] (mailto:bboyjanhuang@gmail.com) 或 [Sina Weibo: Coding Man] (http://weibo.com/ecbboyjan)。
联系
如果在使用过程中遇到问题,请联系: bboyjanhuang@gmail.com。微博:[Coding Man] (http://weibo.com/ecbboyjan)