Laravel 5.0 custom 404 does not use middleware

I use middleware to analyze template output. This works great for all pages.

However, when I want to show 404 (there is a special page for this), it does not treat it as an HTTP request (this is what I think), because it does not go through middleware.

My question is how to have ALL PROGRAMS going through middleware.

+2
middleware laravel
source share
2 answers

Error pages do not go through route.php.

In Kernel.php move your middleware from the $routeMiddleware array to the $middleware array.

The average software in this array will be executed for each request (checked in 5.1).

Image showing middleware and a 404 page

+2
source share

In Laravel 5.4 and possibly some older ones, you can modify the app/exceptions/Handler.php and the render function as follows:

 if( is_a( $exception, \Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class ) ) { return redirect()->route( 'error_404' ); } // ... old code in the function ... 

thus, every 404 errors are redirected to a specific real route, which acts like other site routes.

You can also send any data from the current request to show a reasonable error in the target.

+1
source share

All Articles