🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## composer 安装FastRoute ~~~ composer require nikic/fast-route ~~~ ## 用法 这是一个基本的用法示例: ~~~ <?php require '/path/to/vendor/autoload.php'; $dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) { $r->addRoute('GET', '/users', 'get_all_users_handler'); // {id} must be a number (\d+) $r->addRoute('GET', '/user/{id:\d+}', 'get_user_handler'); // The /{title} suffix is optional $r->addRoute('GET', '/articles/{id:\d+}[/{title}]', 'get_article_handler'); }); // Fetch method and URI from somewhere $httpMethod = $_SERVER['REQUEST_METHOD']; $uri = $_SERVER['REQUEST_URI']; // Strip query string (?foo=bar) and decode URI if (false !== $pos = strpos($uri, '?')) { $uri = substr($uri, 0, $pos); } $uri = rawurldecode($uri); $routeInfo = $dispatcher->dispatch($httpMethod, $uri); switch ($routeInfo[0]) { case FastRoute\Dispatcher::NOT_FOUND: // ... 404 Not Found break; case FastRoute\Dispatcher::METHOD_NOT_ALLOWED: $allowedMethods = $routeInfo[1]; // ... 405 Method Not Allowed break; case FastRoute\Dispatcher::FOUND: $handler = $routeInfo[1]; $vars = $routeInfo[2]; // ... call $handler with $vars break; } ~~~ ### 定义路线 路由是通过调用`FastRoute\simpleDispatcher()`函数来定义的,该函数接受一个可调用`FastRoute\RouteCollector`实例。通过调用`addRoute()`收集器实例来添加路由: ~~~html $r->addRoute($method, $routePattern, $handler); ~~~ 是一个大写的`$method`HTTP 方法字符串,某个路由应该与之匹配。可以使用数组指定多个有效方法: ~~~html // These two calls $r->addRoute('GET', '/test', 'handler'); $r->addRoute('POST', '/test', 'handler'); // Are equivalent to this one call $r->addRoute(['GET', 'POST'], '/test', 'handler'); ~~~ 默认情况下,`$routePattern`使用语法 where`{foo}`指定占位符名称`foo`并匹配正则表达式`[^/]+`。要调整占位符匹配的模式,您可以通过编写指定自定义模式`{bar:[0-9]+}`。一些例子: ~~~html // Matches /user/42, but not /user/xyz $r->addRoute('GET', '/user/{id:\d+}', 'handler'); // Matches /user/foobar, but not /user/foo/bar $r->addRoute('GET', '/user/{name}', 'handler'); // Matches /user/foo/bar as well $r->addRoute('GET', '/user/{name:.+}', 'handler'); ~~~ 路由占位符的自定义模式不能使用捕获组。例如`{lang:(en|de)}`不是有效的占位符,因为`()`是捕获组。相反,您可以使用`{lang:en|de}`或`{lang:(?:en|de)}`。 此外,包含在中的路由部分`[...]`被认为是可选的,因此`/foo[bar]`将同时匹配`/foo`和`/foobar`。可选部件仅支持尾部位置,不支持路线中间。 ~~~html // This route $r->addRoute('GET', '/user/{id:\d+}[/{name}]', 'handler'); // Is equivalent to these two routes $r->addRoute('GET', '/user/{id:\d+}', 'handler'); $r->addRoute('GET', '/user/{id:\d+}/{name}', 'handler'); // Multiple nested optional parts are possible as well $r->addRoute('GET', '/user[/{id:\d+}[/{name}]]', 'handler'); // This route is NOT valid, because optional parts can only occur at the end $r->addRoute('GET', '/user[/{id:\d+}]/{name}', 'handler'); ~~~ 该`$handler`参数不一定是回调,它也可以是控制器类名或您希望与路由关联的任何其他类型的数据。FastRoute 只告诉您哪个处理程序对应于您的 URI,您如何解释它取决于您。 #### [](https://github.com/nikic/FastRoute#shortcut-methods-for-common-request-methods)常用请求方法的快捷方式 对于`GET`、`POST`、`PUT`、和请求方法`PATCH`,可以使用快捷方式。例如:`DELETE``HEAD` ~~~html $r->get('/get-route', 'get_handler'); $r->post('/post-route', 'post_handler'); ~~~ 相当于: ~~~html $r->addRoute('GET', '/get-route', 'get_handler'); $r->addRoute('POST', '/post-route', 'post_handler'); ~~~ #### [](https://github.com/nikic/FastRoute#route-groups)路由组 此外,您可以在组内指定路由。一个组内定义的所有路由都有一个共同的前缀。 例如,将您的路线定义为: ~~~html $r->addGroup('/admin', function (RouteCollector $r) { $r->addRoute('GET', '/do-something', 'handler'); $r->addRoute('GET', '/do-another-thing', 'handler'); $r->addRoute('GET', '/do-something-else', 'handler'); }); ~~~ 将具有与以下相同的结果: ~~~html $r->addRoute('GET', '/admin/do-something', 'handler'); $r->addRoute('GET', '/admin/do-another-thing', 'handler'); $r->addRoute('GET', '/admin/do-something-else', 'handler'); ~~~ 还支持嵌套组,在这种情况下,所有嵌套组的前缀都被组合在一起。 ### [](https://github.com/nikic/FastRoute#caching)缓存 接受用于定义路由的回调的原因`simpleDispatcher`是允许无缝缓存。通过使用`cachedDispatcher`instead of`simpleDispatcher`你可以缓存生成的路由数据并从缓存的信息构造调度程序: ~~~html <?php $dispatcher = FastRoute\cachedDispatcher(function(FastRoute\RouteCollector $r) { $r->addRoute('GET', '/user/{name}/{id:[0-9]+}', 'handler0'); $r->addRoute('GET', '/user/{id:[0-9]+}', 'handler1'); $r->addRoute('GET', '/user/{name}', 'handler2'); }, [ 'cacheFile' => __DIR__ . '/route.cache', /* required */ 'cacheDisabled' => IS_DEBUG_ENABLED, /* optional, enabled by default */ ]); ~~~ 该函数的第二个参数是一个选项数组,可用于指定缓存文件位置等。 ### [](https://github.com/nikic/FastRoute#dispatching-a-uri)调度 URI 通过调用`dispatch()`创建的调度程序的方法来调度 URI。此方法接受 HTTP 方法和 URI。获取这两位信息(并适当地规范化它们)是您的工作——该库未绑定到 PHP Web SAPI。 该`dispatch()`方法返回一个数组,其第一个元素包含状态代码。它是`Dispatcher::NOT_FOUND`,`Dispatcher::METHOD_NOT_ALLOWED`和之一`Dispatcher::FOUND`。对于方法不允许状态,第二个数组元素包含提供的 URI 允许的 HTTP 方法列表。例如: ~~~ [FastRoute\Dispatcher::METHOD_NOT_ALLOWED, ['GET', 'POST']] ~~~ > **注意:**HTTP 规范要求`405 Method Not Allowed`响应包含`Allow:`标头以详细说明所请求资源的可用方法。使用 FastRoute 的应用程序在中继 405 响应时应使用第二个数组元素添加此标头。 对于 found 状态,第二个数组元素是与路由关联的处理程序,第三个数组元素是占位符名称与其值的字典。例如: ~~~ /* Routing against GET /user/nikic/42 */ [FastRoute\Dispatcher::FOUND, 'handler0', ['name' => 'nikic', 'id' => '42']] ~~~ ### [](https://github.com/nikic/FastRoute#overriding-the-route-parser-and-dispatcher)覆盖路由解析器和调度器 路由过程使用三个组件:路由解析器、数据生成器和调度器。这三个组件遵循以下接口: ~~~html <?php namespace FastRoute; interface RouteParser { public function parse($route); } interface DataGenerator { public function addRoute($httpMethod, $routeData, $handler); public function getData(); } interface Dispatcher { const NOT_FOUND = 0, FOUND = 1, METHOD_NOT_ALLOWED = 2; public function dispatch($httpMethod, $uri); } ~~~ 路由解析器采用路由模式字符串并将其转换为路由信息数组,其中每个路由信息又是其部分的数组。使用示例可以最好地理解该结构: ~~~ /* The route /user/{id:\d+}[/{name}] converts to the following array: */ [ [ '/user/', ['id', '\d+'], ], [ '/user/', ['id', '\d+'], '/', ['name', '[^/]+'], ], ] ~~~ 然后可以将该数组传递给`addRoute()`数据生成器的方法。添加所有路由后`getData()`,将调用生成器的 ,它返回调度程序所需的所有路由数据。此数据的格式未进一步指定 - 它与相应的调度程序紧密耦合。 调度程序通过构造函数接受路由数据并提供`dispatch()`您已经熟悉的方法。 路由解析器可以单独重写(以使用一些不同的模式语法),但是数据生成器和调度器应该始终成对更改,因为前者的输出与后者的输入紧密耦合。生成器和调度器分开的原因是在使用缓存时只需要后者(因为前者的输出是被缓存的。) 当使用上面的`simpleDispatcher`/`cachedDispatcher`函数时,覆盖通过选项数组发生: ~~~html <?php $dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) { /* ... */ }, [ 'routeParser' => 'FastRoute\\RouteParser\\Std', 'dataGenerator' => 'FastRoute\\DataGenerator\\MarkBased', 'dispatcher' => 'FastRoute\\Dispatcher\\MarkBased', ]); ~~~ 上面的选项数组对应于默认值。通过替换`MarkBased`为,`GroupCountBased`您可以切换到不同的调度策略。 ### [](https://github.com/nikic/FastRoute#a-note-on-head-requests)关于 HEAD 请求的注释 HTTP 规范要求服务器同时[支持 GET 和 HEAD 方法](http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1.1 "RFC 2616 第 5.1.1 节"): > 所有通用服务器都必须支持 GET 和 HEAD 方法 为了避免强制用户为每个资源手动注册 HEAD 路由,我们回退到为给定资源匹配可用的 GET 路由。PHP web SAPI 透明地从 HEAD 响应中删除了实体主体,因此这种行为对绝大多数用户没有影响。 但是,在 Web SAPI 环境(例如自定义服务器)之外使用 FastRoute 的实施者不得发送为响应 HEAD 请求而生成的实体主体。如果您是非 SAPI 用户,这是*您的责任*;在这种情况下,FastRoute 没有权限阻止您破坏 HTTP。 最后,请注意应用程序可以始终为给定资源指定自己的 HEAD 方法路由以完全绕过此行为