您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
webman-路由-路由中间件 - 路由分组(嵌套分组)中使用路由中间件的注意事项
发布时间:2022-01-19 22:36:01编辑:雪饮阅读()
注意:
->middleware() 路由中间件作用于 group 分组之后时候,当前路由必须在处于当前分组之下
config/route.php中配置如:
<?php
/**
* This file is part of webman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
use Webman\Route;
Route::group('/blog', function () {
Route::group('/v1', function () {
Route::any('/create', [app\controller\Foo::class, 'view2']);
Route::any('/edit', [app\controller\Foo::class, 'index2']);
Route::any('/view/{id}', [app\controller\Foo::class, 'index']);
});
})->middleware([
app\middleware\ActionHook::class,
app\middleware\ActionHook2::class,
]);
Route::group('/blog2', function () {
Route::group('/v1', function () {
Route::any('/create', [app\controller\Foo::class, 'view2']);
Route::any('/edit', [app\controller\Foo::class, 'index2']);
Route::any('/view/{id}', [app\controller\Foo::class, 'index']);
})->middleware([
app\middleware\ActionHook::class,
app\middleware\ActionHook2::class,
]);
});
请求实例:
[root@localhost ~]# elinks http://127.0.0.1:8787/blog/v1/create --dump
foo view2
[root@localhost ~]# elinks http://127.0.0.1:8787/blog/v1/edit --dump
foo index2
[root@localhost ~]# elinks http://127.0.0.1:8787/blog/v1/view/1 --dump
hello index id:1
[root@localhost ~]# elinks http://127.0.0.1:8787/blog2/v1/create --dump
foo view2 ActionHook2 ActionHook
[root@localhost ~]# elinks http://127.0.0.1:8787/blog2/v1/edit --dump
foo index2 ActionHook2 ActionHook
[root@localhost ~]# elinks http://127.0.0.1:8787/blog2/v1/view/1 --dump
hello index id:1 ActionHook2 ActionHook
可以看到仅第二组路由的中间件生效了,也就是说嵌套时候路由中间件应该配置在最深层的路由分组处。
关键字词:路由,中间件,webman,嵌套,分组,注意,事项
下一篇:webman-路由-路由中间件