hyperf-ext / http-server-router
The named router for Hyperf HTTP server package.
Installs: 1 253
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=7.4
- ext-swoole: >=4.5
- hyperf/di: ~2.2.0
- hyperf/framework: ~2.2.0
- hyperf/http-server: ~2.2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- hyperf/testing: ~2.2.0
- phpstan/phpstan: ^0.12
- swoole/ide-helper: dev-master
README
概要
该组件通过绑定 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();
生成指定路由的 URI
/** * @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`