framework: [5.3] No access to sessions or auth on custom http error pages
- Laravel Version: 5.3.28
- PHP Version: 7.0.8-3ubuntu3 (cli)
- Database Driver & Version: not relevant
Description:
When creating custom Blade views for displaying them to the users when a HTTP error occurs, I cannot access authentication or session variables inside. I’m aware that this happens because the standard “web” middleware does not apply to custom error pages, but I’m not sure if this is intentional. In comparison, the middleware that is applied on every request is also applied on custom pages.
If the behavior is intentional: Is there any way to apply middleware to the error pages, but not calling it on every request?
Steps To Reproduce:
- Create a custom error page, e.g. 404.blade.php and save it under
views/errors/ - Try to access the Auth facade in side (e.g. just output another text on
Auth::check();) - Open up the page by accessing a not-defined URL
Related:
http://stackoverflow.com/questions/41517012/laravel-5-3-use-auth-middleware-on-custom-error-page
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 7
- Comments: 31 (9 by maintainers)
@manniL correct, There’s also another option. This is how I fixed it without jQuery:
I created a custom
StartSessionClass inApp\Http\MiddlewareNotice the way it ignores API Routes.
Then I added that to the global middleware in
kernal.phpand removed the old startsession fromwebNext I created a class called
SessionServiceProvider.phpinApp\ProvidersThen finally in
app.phpreplaceIlluminate\Session\SessionServiceProvider::classwithApp\Providers\SessionServiceProvider::classI recommend doing
composer dump-autoloadandphp artisan optimizein case it doesn’t workThat’s the perfect solution if you want sessions across your whole application. Wouldn’t make sense to not do that actually. 😃
Something I do on one of my apps is actually send an ajax request from the error pages in order to query the session. That’s how the login button on StyleCI is able to show up on error pages.
I already posted the example. I’m not going to write your whole app for you I’m afraid. My description leaves it easy to implement yourself.
Sending AJAX request on 404 to trigger the session? This is nuts!
For those looking for an alternative, non-AJAX solution to this; What I did is create a new middleware group for web-rendered errors in the kernel which are then loaded in dynamically upon error rendering. Implementation details of this can be seen in this commit (Only
Handler.phpandKernel.phpchanges are relevant).Global middlewares are executed on a 404, but route-based middlewares aren’t. That’s because a 404 does not match a route, so there’s no knowledge about which middlewares should execute. You would probably need to modify your exception handler to call the relevant middlewares (at least EncryptCookies and StartSession) when needed.
This is the intended behaviour. You can’t expect laravel to guess whether a root mismatch wants sessions, because then 404s on your api would also start sessions.
@sisve Thanks for the reply! Yes, I forgot to mention that global middleware is executed indeed. I’ve linked my question on StackOverflow which relates to the same topic. I’ve already thought about setting the
StartSessionmiddleware as a global one, but there should be a better solution.Do you have an approach for modifying the exception handler for this particular case?