LogoNavigate back to the homepage

Ordering Laravel middleware priority

Yish
August 17th, 2020 · 1 min read

Ordering the middleware through, I traced the underlying instance Illuminate\Foundation\Http\Kernel

1/**
2 * The priority-sorted list of middleware.
3 *
4 * Forces non-global middleware to always be in the given order.
5 *
6 * @var array
7 */
8protected $middlewarePriority = [
9 \Illuminate\Session\Middleware\StartSession::class,
10 \Illuminate\View\Middleware\ShareErrorsFromSession::class,
11 \Illuminate\Contracts\Auth\Middleware\AuthenticatesRequests::class,
12 \Illuminate\Routing\Middleware\ThrottleRequests::class,
13 \Illuminate\Session\Middleware\AuthenticateSession::class,
14 \Illuminate\Routing\Middleware\SubstituteBindings::class,
15 \Illuminate\Auth\Middleware\Authorize::class,
16];

And I found two methods: prependToMiddlewarePriority and appendToMiddlewarePriority, there is a different thing, array_unshift.

Here is a simple to describe.

1protected $middlewarePriority = [
2 // prepend middleware
3
4
5 \Illuminate\Session\Middleware\StartSession::class,
6 \Illuminate\View\Middleware\ShareErrorsFromSession::class,
7 \Illuminate\Contracts\Auth\Middleware\AuthenticatesRequests::class,
8 \Illuminate\Routing\Middleware\ThrottleRequests::class,
9 \Illuminate\Session\Middleware\AuthenticateSession::class,
10 \Illuminate\Routing\Middleware\SubstituteBindings::class,
11 \Illuminate\Auth\Middleware\Authorize::class,
12
13
14 // append middleware
15];

So, where are we calling the method? App\HttpKernel and overwrite __construct after parent construct.

1public function __construct(Application $app, Router $router)
2{
3 parent::__construct($app, $router);
4
5 $this->appendToMiddlewarePriority(One::class);
6 $this->appendToMiddlewarePriority(Two::class);
7 $this->appendToMiddlewarePriority(Three::class);
8}

More articles from Yish

You should know the tricks of Carbon

That's how to make a time machine in Carbon.

July 30th, 2020 · 1 min read

Python learning resources

I wanna to learn about algorithms and how to thinking flexible, but the courses is implementing python, so I'm quickly learn about python, here are I learn resources.

June 15th, 2020 · 1 min read
© 2019–2020 Yish
Link to $https://twitter.com/yishlaiLink to $https://github.com/Mombuyish