Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Add methods to the HTTP kernel to append middleware relative to other middleware #52897

Open
wants to merge 5 commits into
base: 11.x
Choose a base branch
from

Conversation

ollieread
Copy link
Contributor

@ollieread ollieread commented Sep 24, 2024

This PR allows for the programmatic insertion of middleware into the priority, relative to existing prioritised middleware.

Reasoning

My thought behind this is that third-party package developers can now register the priority of their middleware without requiring the user to mess with it themselves. I am currently developing a package where the priority of middleware will depend on several factors, so mostly, I need this. XD

Methods

Kernel::addToMiddlewarePriorityRelative()

This method adds middleware to the Kernel::$middlewarePriority property relative to other middleware. This method is protected and exists for the other two methods.

Kernel::addToMiddlewarePriorityAfter()

This method adds middleware to the Kernel::$middlewarePriority property after existing middleware.

$kernel->addToMiddlewarePriorityAfter(
    \Illuminate\Routing\Middleware\ValidateSignature::class,
    [
        \Illuminate\Cookie\Middleware\EncryptCookies::class,
        \Illuminate\Contracts\Auth\Middleware\AuthenticatesRequests::class,
    ],
);

The above adds the ValidateSignature middleware to the priority list after the two provided pieces of middleware, ensuring that it will run after them.

This method also accepts a string and not just an array, so you can insert after a single piece of middleware.

Kernel::addToMiddlewarePriorityBefore()

This method adds middleware to the Kernel::$middlewarePriority property before existing middleware.

$kernel->addToMiddlewarePriorityBefore(
    \Illuminate\Routing\Middleware\ValidateSignature::class,
    [
        \Illuminate\Cookie\Middleware\EncryptCookies::class,
        \Illuminate\Contracts\Auth\Middleware\AuthenticatesRequests::class,
    ],
);

The above adds the ValidateSignature middleware to the priority list before the two provided pieces of middleware, ensuring that it will run before them.

This method also accepts a string and not just an array, so you can insert before a single piece of middleware.

@ollieread ollieread force-pushed the middleware-priority-dynamic-controls branch from 290d2de to 7bf2e83 Compare September 24, 2024 11:59
@leewillis77
Copy link
Contributor

Just adding a +1 here that I'd definitely find these methods useful!

@crynobone crynobone changed the title Add methods to the HTTP kernel to append middleware relative to other middleware [11.x] Add methods to the HTTP kernel to append middleware relative to other middleware Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants