我正在使用"laravel/lumen-framework": "5.7.*"
我有两个中间件,第一个中间件AuthTokenAuthenticate
应该应用于所有路由,因此它在bootstrap/app.php
中定义,如
$app->middleware([
App\Http\Middleware\AuthTokenAuthenticate::class
]);
[另一个中间件的定义类似于
$app->routeMiddleware([
'auth.token' => Vendor\Utilities\Middleware\AuthToken::class
]);
并且仅适用于某些特定的路线。
我需要先执行auth.token
,然后再执行AuthTokenAuthenticate
,但由于Lumen首先执行$app->middleware
路线,所以我找不到解决方法。
Laravel具有$middlewarePriority
,这正是我所需要的,但是如何在流明中处理它?
我认为您不希望在Lumen中做到这一点。我建议在router group middleware options旁边使用中间件。
删除全局中间件注册
/ bootstrap / app.php
$app->middleware([
//App\Http\Middleware\AuthTokenAuthenticate::class
]);
将两个中间件都添加到路由中间件中
/ bootstrap / app.php
$app->routeMiddleware([
'auth.token' => Vendor\Utilities\Middleware\AuthToken::class,
'auth.token.authenticate' => App\Http\Middleware\AuthTokenAuthenticate::class
]);
[创建两个路由组:一个仅包含 / routes / web / phpauth.token.authenticate
的组,以及一个同时包含auth.token
和auth.token.authenticate
的组。$router->get('/', ['middleware' => 'auth.token.authenticate', function () use ($router) {
// these routes will just have auth.token.authenticate applied
}]);
$router->get('/authenticate', ['middleware' => 'auth.token|auth.token.authenticate', function () use ($router) {
// these routes will have both auth.token and auth.token.authenticate applied, with auth.token being the first one
}]);
我认为这是获得理想效果的最干净的方法。