流明中间件排序(优先级)

问题描述 投票:5回答:1

我正在使用"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,这正是我所需要的,但是如何在流明中处理它?

php laravel middleware lumen
1个回答
0
投票

我认为您不希望在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
]);

[创建两个路由组:一个仅包含auth.token.authenticate的组,以及一个同时包含auth.tokenauth.token.authenticate的组。

/ routes / web / php

$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 }]);

我认为这是获得理想效果的最干净的方法。
© www.soinside.com 2019 - 2024. All rights reserved.