Laravel在API路由上应用了多个中间件

时间:2019-09-19 18:34:06

标签: php laravel authentication middleware

我创建了一个自定义中间件来检查$request->wantsJson()是否应该允许路由调用该函数。顺序是 1.检查JSON 2.检查身份验证

如何按此顺序实施中间件?我已经尝试了以下方法,但无法正常工作

Route::group(['middleware'  =>  ['auth:api', 'json']], function () {
    Route::group(['prefix'  =>  'V1'], function () {
       Route::post('logout', 'API\V1\AuthController@logout');
    });
});

1 个答案:

答案 0 :(得分:1)

您是否在App\Http\Kernel.php路由中间件中注册了中间件?
https://laravel.com/docs/master/middleware#assigning-middleware-to-routes

protected $routeMiddleware = [
    // ...
    'json' => \App\Http\Middleware\CheckForJson::class,
];

还有一个用于强制非全局中间件的优先级(顺序)的数组。
https://laravel.com/docs/master/middleware#sorting-middleware

protected $middlewarePriority = [
    // ...
    \App\Http\Middleware\CheckForJson::class,
];