fastd/routing

FastD 路由组件

v3.2.6 2022-03-24 07:40 UTC

README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

简单的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\ServerRequestInterfaceFastD\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生态的开发者。

如果你对此感到满意,但不知道如何开始,你可以尝试以下事情

联系

如果在使用过程中遇到问题,请联系: bboyjanhuang@gmail.com。微博:[Coding Man] (http://weibo.com/ecbboyjan)

许可 MIT