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
[5.0] Ability to add Kernel middleware at runtime #6211
Comments
Just call the |
Thats not the same. That middleware doesn't run on every request, right? |
Ahh, yes. I see what you mean now. |
It would be really cool to have a way to do this because the current way to do it relies on modifying the Kernel class. |
By the time the service provider is called, seems to late to add anything into the kernel. Right now, you have to add both a service provider & the middleware config. Would be nice to just add the 1 & have it work. |
Yeh, I agree. |
I don't know this work or not, I think it's possible though. Maybe something like this:
|
I would also like this. This is actually possible in L4, so I'm assuming this request is for L5? In my case, I use a Middleware for https://github.com/barryvdh/laravel-debugbar and https://github.com/barryvdh/laravel-cors because Middleware is run also on errors, and the after-filter isn't. But haven't looked into L5.0 much, so not sure if this isn't possible yet. |
Anyone found a way around this yet from a service provider, until hopefully it's added back in? |
This would be useful for packages that need to add middleware. +1 from me. |
I added a method $this->app['Illuminate\Contracts\Http\Kernel']->addMiddleware('Middleware'); The bootstrap happens before the request get send through the middleware stack. See. protected function sendRequestThroughRouter($request)
{
$this->app->instance('request', $request);
$this->bootstrap();
return (new Pipeline($this->app))
->send($request)
->through($this->middleware)
->then($this->dispatchToRouter());
} |
+1 for this feature from me. |
@barryvdh, from above duplicate issue:
There is already limited control of the ordering of things. First global middleware, then before filters, then route middleware, etc. If your ordering matters, you can always consider not to add the global middleware through your |
so is there no solution? |
None that I'm aware of. |
So far I've only come up with a workaround that only partially solves the issue of ordering. What currently solves my problems is that I'm using an event handler on |
I'm going to submit a pull for this. I agree with the above statement in that if ordering really matters then it can be added manually. Best to probably check in the |
FYI, this has been added (see above PR) This issue can be closed. |
Thanks @barryvdh. |
And @jasonlewis. :) |
is there a way to add $routeMiddelware at runtime? |
I'm trying to figure out where's the best place to add a call to To be precise, I have a project with the clockwork package installed for debugging reasons. But to be honest, I'm struggling to find the correct place to add the next piece of code (or whatever is needed to accomplish this action...): if (config('app.debug'))
{
$kernel->prependMiddleware('Clockwork\Support\Laravel\ClockworkServiceProvider');
} Basically I don't know when/where is the best moment, during the request lifecycle... Btw, I've tried this example but I get an error... |
Please ask on the forums. |
Any plans on getting this fixed for package maintainers in 5.1? |
It has been added we are already using the fix our packages, recheck barrys commit which adds the methods. |
You're right, I was using |
It is great that we now have the ability to add dynamic global middleware with
BUT we still need the ability to define route based middleware as well. This is critical with adding packages that require their own custom middleware. Without this feature, the packages has to resort back to using Laravel 4 Filters. Started a new issues about this at #8917 |
Maybe this one can help you guys.
change the yourroutermiddleware, etc. backendauth sample router
This code is tested and working. |
Yes that is what crynobone's replied in #8917 and it works. So we can dynamically add both global middleware with prependMiddleware() and pushMiddleware() and route based middleware with
|
@GrahamCampbell |
I need to add middleware to the kernel at runtime from a service provider. Something along the lines of:
The text was updated successfully, but these errors were encountered: