Laravel 5.0 custom 404 does not use middleware

拥有回忆 提交于 2019-12-11 00:01:51

问题


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.


回答1:


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).




回答2:


At Laravel 5.4 and probably some older ones you can modify the file app/exceptions/Handler.php and the function render like this:

if( is_a( $exception, \Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class ) ) {
    return redirect()->route( 'error_404' );
}

// ... old code in the function ...

in this way every 404 errors are redirected to certain real route that acts like other routes of site.

You may also submit any data from current request to show a reasonable error at the target.



来源:https://stackoverflow.com/questions/30765230/laravel-5-0-custom-404-does-not-use-middleware

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!