webman 使用嵌套路由后部分路由不會(huì)通過(guò)中間件
路由配置
Route::group('/api', function () {
Route::any('/test1', function () {
return 'test1';
});
Route::group('/group', function () {
Route::any('/test2', function () {
return 'test2';
});
});
Route::any('/test3', function () {
return 'test3';
});
})->middleware(TestMiddleware::class);
中間件配置
<?php
namespace app\middleware;
use Webman\Http\Request;
use Webman\Http\Response;
use Webman\MiddlewareInterface;
class TestMiddleware implements MiddlewareInterface
{
public function process(Request $request, callable $handler): Response
{
return json([false]);
}
}
理論上來(lái)說(shuō)訪問(wèn)每個(gè)路由都應(yīng)該返回 [false]
但實(shí)際,在第二層group之前的路由正常通過(guò)了中間件,返回[false],后續(xù)(包括group中的)未被攔截(實(shí)測(cè)為未通過(guò)中間件),如圖所示
curl 127.0.0.1:8787/api/test1
curl 127.0.0.1:8787/api/group/test2
curl 127.0.0.1:8787/api/test3
WSL 最新 webman
http://wtbis.cn/doc/webman/route.html#%E8%B7%AF%E7%94%B1%E4%B8%AD%E9%97%B4%E4%BB%B6
手冊(cè)說(shuō)了不支持你這種,正確寫法
Route::group('/api', function () {
Route::any('/test1', function () {
return 'test1';
});
Route::any('/test3', function () {
return 'test3';
});
})->middleware(TestMiddleware::class);
Route::group('/api', function () {
Route::group('/group', function () {
Route::any('/test2', function () {
return 'test2';
});
})->middleware(TestMiddleware::class);
});