Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.0 custom 404 does not use middleware

I'm using a middleware to parse the output of the templates. This is working fine for all pages.

However when I want to show a 404 (got a custom page for that) it doesn't treat it as a http request (that's what I think) since it doesn't go through the middleware.

My question is, how to have ALL requests go through the middleware.

like image 686
Wouter Rutgers Avatar asked Jun 10 '15 19:06

Wouter Rutgers


People also ask

How do I create a custom 404 page in Laravel?

Create Custom 404 Error Page You need to create blade views for error pages, move to this path resources/views/ inside here create errors folder and within the directory create 404. blade. php file. It will redirect you to the 404 page if you don't find the associated URL.

How do I customize my 404 page in Laravel 8?

The first step you need to create is directory in resources/views and then create a directory named as errors . Once you created the directory, you can add the error pages like 404, 500, 501, 505, 400 etc. By default laravel provides its own error pages and we can customize them using this directory.

How can you assign middleware in Laravel?

Assigning Middleware To Routes If you would like to assign middleware to specific routes, you should first assign the middleware a key in your application's app/Http/Kernel.php file. By default, the $routeMiddleware property of this class contains entries for the middleware included with Laravel.

Where we can use middleware in Laravel?

There are two types of Middleware in Laravel. The Global Middleware will run on every HTTP request of the application, whereas the Route Middleware will be assigned to a specific route. The middleware can be registered at app/Http/Kernel.


1 Answers

The error pages don't go through the routes.php.

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

Middleware in this array will run on every request (tested in 5.1).

Image showing middleware and a 404 page

like image 164
Björn Avatar answered Nov 15 '22 07:11

Björn