The execution priorities of Laravel’s middlewares

The execution priorities of Laravel’s middlewares

Laravel 多个中间件的执行顺序

问题
一个路由需要用到多个中间件,其中一个是 Laravel 自带的 auth 中间件。

发现这个中间件不管放在哪里,总是在自定义中间件之前执行。

如果业务需要自定义中间在 auth 之前执行,还是有办法的。

解决方案
观察定义中间件的 app\Http\Kernel 类,是继承的 Illuminate\Foundation\Http\Kernel 类。

再打开 Illuminate\Foundation\Http\Kernel ,发现有这样一个数组

...

/**
 * The priority-sorted list of middleware.
 *
 * Forces the listed middleware to always be in the given order.
 *
 * @var array
 */
protected $middlewarePriority = [
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    \Illuminate\Auth\Middleware\Authenticate::class,
    \Illuminate\Session\Middleware\AuthenticateSession::class,
    \Illuminate\Routing\Middleware\SubstituteBindings::class,
    \Illuminate\Auth\Middleware\Authorize::class,
];

...

注释里写得比较清楚了:列表中的中间件会按照顺序优先在其他中间件之前执行。

那么需要自定义中间件在 auth 之前执行,只需要在 app\Http\Kernel 重载这个数组就行了。

auth 中间件对应的类 \Illuminate\Auth\Middleware\Authenticate,

将自定义中间件 \App\Http\Middleware\CustomMiddelware 放到这个前面就可以了:

...

protected $middlewarePriority = [
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    \App\Http\Middleware\CustomMiddelware::class,
    \Illuminate\Auth\Middleware\Authenticate::class,
    \Illuminate\Session\Middleware\AuthenticateSession::class,
    \Illuminate\Routing\Middleware\SubstituteBindings::class,
    \Illuminate\Auth\Middleware\Authorize::class,
];

...

拓展

不在 $middlewarePriority 列表中的中间件,是按照在路由配置里调用的顺序来的:

在 Route::group 里定义的,先执行外层,后执行内层

在数组定义的,先执行写在数组前面的,后执行写在数组后面的

举例:

Route::group([‘middleware’ => ‘outer’], function () {
Route::group([‘middleware’ => ‘inner’], function () {
Route::group([‘middleware’ => [‘array1’, ‘array2’, ‘auth:api’]], function () {
Route::get(‘test’, function () {
return;
});
});
});
});

这里就是按照 outer 、 inner 、 array1 、 array2 的顺序执行。

原文:https://blog.csdn.net/realghost/article/details/80969253

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.