Laravel 5.3: use auth / middleware on user errors page

Problem

When displaying HTTP error pages (404, 500, etc.), I want to keep the standard project of my current project, including the header and footer. My project also includes a registration system. When a user logs in and receives an error message, he will be redirected to the corresponding error page, but Laravel will not recognize that the user is registered . This is because custom error pages (located in resources/views/errors/{code}.blade.php do not start through regular middleware (for some reason).

This behavior has already been reported several times, but no response has been received. The hacking solution is to install the StartSession for each request, but this is not enough for my case.

How can I use Auth / Session middleware on user error pages?

Inappropriate decisions

  • I don't want to add StartSession (or any other) to every request

Related Questions and Links

  • Laravel Auth on error pages , Laravel 5.0 custom 404 does not use middleware → The solution did not want
  • Laravel 5.2 Auth :: check () on exception pages (layouts) There is no good solution
  • GitHub issue that I discovered because I think this behavior is not intentional
+7
php error-handling middleware laravel laravel-5
source share
2 answers

In line with the discussion of the github issue, I opened, here are two best practices:

  • Adding StartSession to the list of global middlewares is a good estimate if your application does not have an API or the like
  • In the second case, you can request the necessary Frontend elements that are affected by the session (for example, login / register or profile buttons) using jQuery and an AJAX call. This way you invoke the route that the middleware uses, and therefore get the correct elements. A good example can be found in the GitHub issue I linked.
0
source share

Why don't you choose the error pages by expanding the main layout file instead of including the header and footer. In my case, the design is kept unified by encoding the 404 page, as shown below:

 @extends('layouts.app') @section('content') <div class="row"> <div class="col-md-8"> <div class="widget widget-default"> <h1>404...Nothing here</h1> </div> </div> <div class="col-md-4"> @include('widget.user') @include('widget.categories') </div> </div> @endsection 
-one
source share

All Articles