该组件通过绑定 HyperfExt\HttpServer\Router\DispatcherFactory
到 Hyperf\HttpServer\Router\DispatcherFactory
来实现扩展路由功能,由于修改了返回类型,PHP 版本必须 >= 7.4。
composer require hyperf-ext/http-server-router
在路由选项中定义 name
参数来对路由命名,支持对路由组命名。
use Hyperf\HttpServer\Router\Router;
Router::addGroup('/users/{id}', function () {
Router::get('/comments', 'App\Controller\IndexController@index', ['name' => 'comments.index']); // 该路由名称将被组合为 `users.comments.index`
}, ['name' => 'users.']);
use Hyperf\HttpServer\Router\Router;
/** @var \HyperfExt\HttpServer\Router\Route $route */
$route = Router::getRoute('users.comments.index');
use Hyperf\HttpServer\Router\Router;
/** @var \HyperfExt\HttpServer\Router\Route $route */
$route = Router::getCurrentRoute();
/**
* @var \HyperfExt\HttpServer\Router\Route $route
* @var \Hyperf\HttpMessage\Uri\Uri $uri
*/
$uri = $route->createUri([
'id' => 123,
'page_num' => 2,
'page_size' => 20,
]);
$link = (string) $uri; // 结果为 `/users/123/comments?page_num=2&page_size=20`