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

pushMiddleware changed order #15352

Closed
mreschke opened this issue Sep 8, 2016 · 3 comments
Closed

pushMiddleware changed order #15352

mreschke opened this issue Sep 8, 2016 · 3 comments

Comments

@mreschke
Copy link

mreschke commented Sep 8, 2016

How can I add middleware to the end of the full stack from a service provider in Laravel 5.3?

In Laravel 5.1 all of my "packages" pushed their own middleware in their service providers boot() method like so $kernel->pushMiddleware('Mrcore\Wiki\Http\Middleware\AnalyzeRoute');

This would push middleware to the END of the FULL stack, including the stack listed in the main app/Http/Kernel.php. So dump($kernel) would reveal the proper order

0 => "Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode"
1 => "App\Http\Middleware\EncryptCookies"
2 => "Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse"
3 => "Illuminate\Session\Middleware\StartSession"
4 => "Illuminate\View\Middleware\ShareErrorsFromSession"
5 => "Mrcore\Wiki\Http\Middleware\AnalyzeRoute"

This was ideal because at the custom middleware level Auth:: and Session:: are already fired up.

But now in Laravel 5.2 and above, when I ->pushMiddleware it adds it BEFORE it appends any app/Http/Kernel.php so a dump($kernel) at the provider level looks like so

0 => "Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode"
1 => "Mrcore\Wiki\Http\Middleware\AnalyzeRoute"

This means that any custom middleware you want to define from the service provider has no concept of Session or Auth yet.

Is there a particular reason for this change?

And I must continue defining the "packages" middleware from the provider as the main laravel app cannot be modified by the users. This is a CMS, so all must be defined in the package.

This is somewhat related to #15072 in how Auth has moved around but is NOT the same issue, so please don't disregard as "closed".

@themsaid
Copy link
Member

@mreschke
The thing is this has been the case since 5.2, if we try to fix the order now it might break other applications that depend on the new sort of order.

What if you use Router::pushMiddlewareToGroup and push your middlware to the web and api groups, would that solve your issue?

@mreschke
Copy link
Author

@themsaid wow thank you so much. That is exactly what was need to get the middleware back in the same order.

So from any "apps" provider boot() I can now $router->pushMiddlewareToGroup('web', \Mrcore\Wiki\Http\Middleware\AnalyzeRoute::class); which will indeed push back to end of web group as it used too in laravel 5.1.

dump($router)
  #middlewareGroups: array:2 [▼
    "web" => array:6 [▼
      0 => "App\Http\Middleware\EncryptCookies"
      1 => "Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse"
      2 => "Illuminate\Session\Middleware\StartSession"
      3 => "Illuminate\View\Middleware\ShareErrorsFromSession"
      4 => "Illuminate\Routing\Middleware\SubstituteBindings"
      5 => "Mrcore\Wiki\Http\Middleware\AnalyzeRoute"
    ]
    "api" => array:2 [▶]
  ]

@themsaid
Copy link
Member

@mreschke Glad I could help :)

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

No branches or pull requests

2 participants