您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP路由库FastRoute的使用教程

2025/11/5 13:30:25发布20次查看
github: https://github.com/nikic/fastroute
这个库提供了基于正则表达式的快速路由实现。这篇文章解释了 fastroute 是如何工作的和它为什么很快。
安装
通过 composer 安装
composer require nikic/fast-route
要求 php 5.4 及更高的版本
使用
这是一个基本的使用示例
<?phprequire '/path/to/vendor/autoload.php';$dispatcher = fastroute\simpledispatcher(function(fastroute\routecollector $r) { $r->addroute('get', '/users', 'get_all_users_handler'); // {id} 必须是一个数字 (\d+) $r->addroute('get', '/user/{id:\d+}', 'get_user_handler'); // /{title} 后缀是可选的 $r->addroute('get', '/articles/{id:\d+}[/{title}]', 'get_article_handler');});// 获取请求的方法和 uri$httpmethod = $_server['request_method'];$uri = $_server['request_uri'];// 去除查询字符串( ? 后面的内容) 和 解码 uriif (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 实例为参数的闭包作为参数。通过在 collector 实例里面调用 addroute() 增加路由。
$r->addroute($method, $routepattern, $handler);
$method 是大写的 http 方法,能够被某个路由匹配,可以使用数组指定多个有效的 $method 。
// 这里两行调用$r->addroute('get', '/test', 'handler');$r->addroute('post', '/test', 'handler');// 等同于这一行调用$r->addroute(['get', 'post'], '/test', 'handler');
默认情况下 $routepattern 使用一种语法,比如 {foo} 是指定名称为 foo 的占位符,可以匹配正则表达式 [^/]+. 。要调整占位符匹配的模式,可以通过编写 {bar:[0-9] +} 来指定自定义模式。一些例子
// 匹配 /user/42,不匹配 /user/xyx$r->addroute('get', '/user/{id:\d+}', 'handler');// 匹配 /user/foobar,不匹配 /user/foo/bar$r->addroute('get', '/user/{name}', 'handler');// 匹配 /user/foobar,也匹配 /user/foo/bar$r->addroute('get', '/user/{name:.+}', 'handler');
路由占位符的自定义模式不能使用捕获组,例如 {lang:(en|de)} 不是有效的占位符,因为 () 是一个捕获组,可以使用 {lang:en|de} 或者 {lang:(?:en|de)} 代替。
另外,在路由 [...] 中定义的部分是可选匹配的,所以 /foo[bar] 将匹配 /foo 和 /foobar 。路由可选部分只支持在定义的末尾,而不能在定义的中间。
// 这个路由有,[/{name}] 可选择匹配部分$r->addroute('get', '/user/{id:\d+}[/{name}]', 'handler');// 等同于这两个路由$r->addroute('get', '/user/{id:\d+}', 'handler');$r->addroute('get', '/user/{id:\d+}/{name}', 'handler');// 多层嵌套可选路由,也是支持的$r->addroute('get', '/user[/{id:\d+}[/{name}]]', 'handler');// 这个路由定义无效,因为可选部分只能在定义的末尾$r->addroute('get', '/user[/{id:\d+}]/{name}', 'handler');
$handler 参数不一定必须是回调函数,它也可以是控制器类名或任何其他类型的数据。fastroute 只告诉你哪个 handler 对应 uri,如何解释它取决于你。
请求方法的书写快捷方式
对于 get、post、put、patch、delete 和 head 请求方法,可使用快捷方式。
$r->get('/get-route', 'get_handler');$r->post('/post-route', 'post_handler');// 等同于$r->addroute('get', '/get-route', 'get_handler');$r->addroute('post', '/post-route', 'post_handler');
路由组
你可以在一个组内定义路由,同一组内的路由有相同的前缀。
$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');});// 等同于$r->addroute('get', '/admin/do-something', 'handler');$r->addroute('get', '/admin/do-another-thing', 'handler');$r->addroute('get', '/admin/do-something-else', 'handler');
可以定义多层嵌套组结构。
缓存
使用 simpledispatcher 定义路由的回调函数可以无缝缓存。通过使用 cacheddispatcher 而不是 simpledispatcher,可以缓存生成的路由数据并从缓存的信息构建调度。
<?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 是否缓存,可选参数,默认情况下开启 */]);
该函数的第二个参数是一个选项数组,可用于指定缓存文件路径等等。
调度 uri
通过调用 dispatch() 调度 uri。这个方法接受 http 方法 和一个 uri 作为参数。获得这两个信息是你自己的工作,这个库并不绑定到 php web sapis 。
dispatch() 返回一个数组,第一个元素是一个状态码,状态码是 dispatcher::not_found、dispatcher::method_not_allowed、dispatcher::found 其中之一。对于 dispatcher::method_not_allowed 状态,第二个数组元素包含允许提供的 uri 的 http 方法列表。
[fastroute\dispatcher::method_not_allowed, ['get', 'post']]
对于 dispatcher::found 状态,第二个数组元素是 $handler ,第三个数组元素是是一个包含所有占位符的数组
/* routing against get /user/nikic/42 */[fastroute\dispatcher::found, 'handler0', ['name' => 'nikic', 'id' => '42']]
重写路由解析器和调度器
这个库使用三个组件,一个路由解析器,一个数据生成器,一个调度器。这个三个组件实现以下接口
<?phpnamespace 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 时,可以通过传入额外的参数,进行覆盖
<?php$dispatcher = fastroute\simpledispatcher(function(fastroute\routecollector $r) { /* ... */}, [ 'routeparser' => 'fastroute\\routeparser\\std', 'datagenerator' => 'fastroute\\datagenerator\\groupcountbased', 'dispatcher' => 'fastroute\\dispatcher\\groupcountbased',]);
上面给出了默认的设置,通过把 groupcountbased 替换成 groupposbased 可以使用完全不同的调度策略
关于head请求的说明
http 规范要求服务器 同时支持 get 和 head 方法
get和head方法必须得到所有通用服务器的支持
为避免强制用户为每个资源手动注册 head 路由,将使用一个匹配的 get 路由响应请求。php web sapi 透明地从 head 响应中移除实体主体,所以这种行为对绝大多数用户没有影响。
但是,在 web sapi 环境外部使用 fastroute ,绝不能发送响应 head 请求而生成的实体主体,如果你是非 sapi 用户,这是你的责任;在这种情况下,fastroute 无权限制你破坏 http 。
最后,请注意,应用程序可以始终为给定资源指定其自己的 head 方法路由以完全绕过此行为。
推荐教程:《php教程》
以上就是php路由库fastroute的使用教程的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product