阅读(2718) (7)

Laravel 8 中间件排序

2021-06-24 09:19:01 更新

很少情况下,你可能需要中间件以特定的顺序执行,但是当它们被分配到路由时,你无法控制它们的顺序。在这种情况下,可以使用 app/Http/Kernel.php 文件中的 $middlewarePriority 属性指定中间件的优先级:

/**
 * 中间件的优先级排序列表
 *
 * 将会强制非全局中间件始终保持给定的顺序
 *
 * @var array
 */
protected $middlewarePriority = [
    IlluminateSessionMiddlewareStartSession::class,
    IlluminateViewMiddlewareShareErrorsFromSession::class,
    IlluminateContractsAuthMiddlewareAuthenticatesRequests::class,
    IlluminateRoutingMiddlewareThrottleRequests::class,
    IlluminateSessionMiddlewareAuthenticateSession::class,
    IlluminateRoutingMiddlewareSubstituteBindings::class,
    IlluminateAuthMiddlewareAuthorize::class,
];