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

Add the ability to append and prepend middleware priority from the application builder #53326

Merged

Conversation

ollieread
Copy link
Contributor

@ollieread ollieread commented Oct 28, 2024

This is a follow-up PR for #52897 that adds two methods to the application builder, allowing for easy access to the add middleware after/before methods on the kernel.

Using the examples from the previous PR, the methods would be accessible like so:

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->appendToPriorityList(
		    [
		        \Illuminate\Cookie\Middleware\EncryptCookies::class,
		        \Illuminate\Contracts\Auth\Middleware\AuthenticatesRequests::class,
		    ],
		    \Illuminate\Routing\Middleware\ValidateSignature::class
		);

		$middleware->prependToPriorityList(
		    [
		        \Illuminate\Cookie\Middleware\EncryptCookies::class,
		        \Illuminate\Contracts\Auth\Middleware\AuthenticatesRequests::class,
		    ],
		    \Illuminate\Routing\Middleware\ValidateSignature::class
		);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();

They map as follows:

  • Middleware::appendToPriorityList() -> Kernel::addToMiddlewarePriorityAfter()
  • Middleware::prependToPriorityList() -> Kernel::addToMiddlewarePriorityBefore()

@taylorotwell
Copy link
Member

taylorotwell commented Oct 29, 2024

Can the signature be something like:

$middleware->appendToPriorityList(after: $middlewareToPutTheNewMiddlewareAfter, append: $theMiddlewareToAppend);
$middleware->prependToPriorityList(before: $middlewareToPutTheNewMiddlewareBefore, prepend: $theMiddlewareToPrepend);

Please mark as ready for review when the requested changes have been made.

@taylorotwell taylorotwell marked this pull request as draft October 29, 2024 14:49
@ollieread ollieread marked this pull request as ready for review October 30, 2024 13:28
@taylorotwell
Copy link
Member

Hey there! I'm not sure if the parameter names match what I mentioned?

@taylorotwell taylorotwell marked this pull request as draft October 30, 2024 14:12
@ollieread
Copy link
Contributor Author

Hey there! I'm not sure if the parameter names match what I mentioned?

Oh sorry, I had assumed they were just to illustrate as the names are quite long. No worries, I'll update that now.

@ollieread
Copy link
Contributor Author

Hey there! I'm not sure if the parameter names match what I mentioned?

All done!

@ollieread ollieread marked this pull request as ready for review October 30, 2024 14:54
@taylorotwell
Copy link
Member

Sorry, I mean the actual parameter names, which are here:

CleanShot 2024-10-30 at 09 57 11@2x

@taylorotwell taylorotwell marked this pull request as draft October 30, 2024 14:57
@ollieread
Copy link
Contributor Author

Sorry, I mean the actual parameter names, which are here:

CleanShot 2024-10-30 at 09 57 11@2x

Ah, that makes so much more sense. Sorry, that's on me, my head is all over the place today!

@ollieread ollieread marked this pull request as ready for review October 30, 2024 15:01
@taylorotwell taylorotwell merged commit 942b5a0 into laravel:11.x Oct 31, 2024
31 checks passed
@ollieread ollieread deleted the feat/middleware-priority-configuration branch October 31, 2024 20:06
Copy link
Contributor

@patrickomeara patrickomeara left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs tests as I have tried to move my ->priority() call to use this but the priority doesn't change.

Additionally there is an issue with using both as below.

Comment on lines +273 to +283
if ($priorityAppends = $middleware->getMiddlewarePriorityAppends()) {
foreach ($priorityAppends as $middleware => $after) {
$kernel->addToMiddlewarePriorityAfter($after, $middleware);
}
}

if ($priorityPrepends = $middleware->getMiddlewarePriorityPrepends()) {
foreach ($priorityPrepends as $middleware => $before) {
$kernel->addToMiddlewarePriorityBefore($before, $middleware);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using both prependToPriorityList() and appendToPriorityList() the $middleware variable is overridden on line 274. getMiddlewarePriorityPrepends() is then called on a string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah this seems to be a bug either prepending!

@ollieread
Copy link
Contributor Author

I think this needs tests as I have tried to move my ->priority() call to use this but the priority doesn't change.

Additionally there is an issue with using both as below.

I suspect this is because of the bug you mentioned. I've created a PR fix at #53504

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.

3 participants