docs(router): update router documentation (#6353)

This commit is contained in:
Binary Alan 2023-12-05 18:02:00 +08:00 committed by GitHub
parent 50b7fa9f37
commit 4081f331d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 61 additions and 1 deletions

View File

@ -69,6 +69,8 @@ When defining routes through annotations, we recommend defining middleware by me
> Use `#[Middleware]` should `use Hyperf\HttpServer\Annotation\Middleware;` namespace
> Use `#[Middlewares]` should `use Hyperf\HttpServer\Annotation\Middlewares;` namespace
***Notice: It must be used with `#[AutoController]` or `#[Controller]`.***
Define a single middleware
```php

View File

@ -203,6 +203,34 @@ We can define required route parameters using `{}`. For example, `/user/{id}` de
Sometimes you may want a route parameter to be optional. In this case, you can use `[]` to declare the parameter inside the brackets as an optional parameter, such as `/user/[{id}]`.
#### Validate parameters
You can also use regular expression to validate parameters. Here are some examples
```php
use Hyperf\HttpServer\Router\Router;
// Matches /user/42, but not /user/xyz
Router::addRoute('GET', '/user/{id:\d+}', 'handler');
// Matches /user/foobar, but not /user/foo/bar
Router::addRoute('GET', '/user/{name}', 'handler');
// Matches /user/foo/bar as well
Router::addRoute('GET', '/user/{name:.+}', 'handler');
// This route
Router::addRoute('GET', '/user/{id:\d+}[/{name}]', 'handler');
// Is equivalent to these two routes
Router::addRoute('GET', '/user/{id:\d+}', 'handler');
Router::addRoute('GET', '/user/{id:\d+}/{name}', 'handler');
// Multiple nested optional parts are possible as well
Router::addRoute('GET', '/user[/{id:\d+}[/{name}]]', 'handler');
// This route is NOT valid, because optional parts can only occur at the end
Router::addRoute('GET', '/user[/{id:\d+}]/{name}', 'handler');
```
#### Get routing information
If the devtool component is installed, you can use the `php bin/hyperf.php describe:routes` command to get the routing list information. You can also provide the path option, which is convenient for obtaining the information of a single route, for example: `php bin/hyperf.php describe:routes --path=/foo/bar`.

View File

@ -69,6 +69,8 @@ Router::addGroup(
> 使用 `#[Middleware]` 注解时需 `use Hyperf\HttpServer\Annotation\Middleware;` 命名空间;
> 使用 `#[Middlewares]` 注解时需 `use Hyperf\HttpServer\Annotation\Middlewares;` 命名空间;
***注意:必须配合`#[AutoController]`或者`#[Controller]`使用***
定义单个中间件:
```php

View File

@ -202,6 +202,34 @@ public function index(RequestInterface $request)
有时候您可能会希望这个参数是可选的,您可以通过 `[]` 来声明中括号内的参数为一个可选参数,如 `/user/[{id}]`
#### 校验参数
您也可以使用正则表达式对参数进行校验,以下是一些例子
```php
use Hyperf\HttpServer\Router\Router;
// 可以匹配 /user/42, 但不能匹配 /user/xyz
Router::addRoute('GET', '/user/{id:\d+}', 'handler');
// 可以匹配 /user/foobar, 但不能匹配 /user/foo/bar
Router::addRoute('GET', '/user/{name}', 'handler');
// 也可以匹配 /user/foo/bar as well
Router::addRoute('GET', '/user/{name:.+}', 'handler');
// 这个路由
Router::addRoute('GET', '/user/{id:\d+}[/{name}]', 'handler');
// 等同于以下的两个路由
Router::addRoute('GET', '/user/{id:\d+}', 'handler');
Router::addRoute('GET', '/user/{id:\d+}/{name}', 'handler');
// 多个可选的嵌套也是允许的
Router::addRoute('GET', '/user[/{id:\d+}[/{name}]]', 'handler');
// 这是一条无效的路由, 因为可选部分只能出现在最后
Router::addRoute('GET', '/user[/{id:\d+}]/{name}', 'handler');
```
#### 获取路由信息
如果安装了 devtool 组件,可使用 `php bin/hyperf.php describe:routes` 命令获取路由列表信息,

View File

@ -31,4 +31,4 @@ return [
'a.b.php中的配置项' => 'a.b.php中的配置值',
]
];
```
```