tuum / router
PHP 简单路由器
README
另一个小巧的 PHP 路由组件。
MIT 许可证
为什么还需要另一个路由组件?
我在寻找一个能以单行代码工作的路由组件,并且具有以下特性:
- 匹配方法和以字符串表示的路由模式,
- 匹配参数化的标记以及路径的开始,并且
- 代码库非常小,
但我找不到这样的组件。虽然可以使用正则表达式来完成上述任务,但我发现每次编写正确的正则表达式都太困难了。
匹配器
基本用法
use Tuum\Routing\Matcher; $matched = Matcher::verify( $pattern, $path, $method);
如果 $path 与 $pattern 匹配,则方法返回匹配信息的数组。否则返回空数组。
示例
匹配方法 get
,整数参数 id
。
$matched = Matcher::verify('get:/path/{id:i}', '/path/1001', 'get'); if ($matched) { $id = $matched['id']; }
匹配任何方法,以 /path/to
开始。
$matched = Matcher::verify('/path/to/*', '/path/to/more/route', 'ignored'); if ($matched) { echo $matched['matched']; // '/path/to/' echo $matched['trailing']; // 'more/route' }
Matcher 类没有依赖项,包括注释在内代码少于 100 行。
闭包样式
既可以作为静态方法,也可以作为闭包样式提供。
$matcher = new Matcher; $matched = $matcher('/my/resource/*', '/my/resource/1001', 'get');
返回值
Matcher::verify
方法返回匹配参数的数组,如果匹配失败则返回空数组。
返回值是 preg-match 结果以及更多内容。请勿使用字符串作为参数。
- method: 匹配的方法。如果没有指定方法,则此值可能为空。
- matched: 如果使用
{*}
,则匹配基础路径。 - trailing: 如果使用
{*}
,则匹配剩余路径。
可用的路由模式
路由模式。
pattern := ([method]:)[route](*)
route
一般来说,路由模式以斜杠 ('/') 开头。如果路由包含冒号 (':') 但不想指定方法,请确保路由以斜杠开头。
parameter
语法是 {parameter_name(:type)}
。
参数名必须只包含字母和下划线 ([_0-9a-zA-Z]+),并且只匹配字母、连字符和下划线 ([-_0-9a-zA-Z]+)。
目前,仅支持 'i' 类型的整数,即 /resource/{id:i}
。
method
方法名必须只由字母组成。(方法名不能包含冒号。)
要忽略匹配中的方法,请使用星号 (*) 作为方法值,例如;
Matcher::verify( 'get:/path/{id}', '/path/1234', '*' );
尾部路由 (*)
要匹配任何剩余路由,请使用 *,
- 对于任何路径:
/path/to/*
,它仅匹配任何路由。 - 对于匹配尾部路由:
/path/to/{*}
,如果匹配,则返回匹配的路由作为matched
,剩余的路由作为trailing
。
路由器类
以下实现是为了使它看起来像是一个包。
$router = new Router([ 'get:/path/' => 'index', 'get:/path/{id}' => 'get', ]); $router->addRoute('put:/path/{id}', 'put'); $matched = $router->match('/path/123'); if($matched) { $method = $matched[0]; // the handler $params = $matched[1]; // matched parameter $id = $params['id']; }
路由器类接受一个模式和它的 handler
的数组,并与路径进行匹配。一个 addRoute
方法可以逐个添加模式和 handler
。
如果匹配,则路由器返回一个包含 handler 和匹配参数的数组。
handler 可以是任何东西。它只需返回设置的任何内容。
路由集合和处理器类
使用 RouteCollection 和 Handler 对象轻松创建模式。
$router = new Router(); $routes = $router->getRouting(); $routes->any('/', 'top') ->name('top'); $routes->get('/welcom', function(){ echo 'welcome';}) ->before('UserNameFilter'); $matched = $router->match('/welcom', 'get'); echo get_class($matched[0]); // Tuum\Routing\Handler
目前,$router 返回与任何其他类似的匹配结果作为数组。但如果直接返回 Handle 对象(如果 handler 是 Handle 对象)可能会更简单...
路由类
如果使用 RouteCollection(即 handler 是 $handler),则 Router::match
方法将返回 Route
类。
处理器和路由 API 列表
$routes
(RouteCollector)使用 Handler
对象来设置路由信息。
Handler 有以下方法。
$routes->{method}($routePattern, $handler) ->name($route_name) ->before($filter_name) ->after($may_not_work) ->params($default_parameter);
路由类有以下方法用于读取匹配信息。
echo $route->handle(); echo $route->name(); echo $route->before(); echo $route->after(); echo $route->params(); echo $route->path(); echo $route->method(); echo $route->trailing(); echo $route->matched();
分组路由
使用 group
方法为多个路由分配相同的属性和/或匹配模式,如下所示。
$routes->group([ 'pattern' => '/admin/', 'handler' => 'Admin\Controller\', 'before' => 'AdminAuth', ], function($routes) { /** @var RouteCollector $routes */ $routes->get('/', 'MainController'); }); $matched = $route->match('/admin/', 'get');
ReverseRoute
待修改。
用法
$router = new Route(); $routes = $router->getRouting(); $routes->get('/sample/{id}', 'sample' )->name('sample'); $reverse = $router->getReverseRoute(); $route = $reverse->generate('sample',['id'=>'123']);