Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.4 | Custom Page for NotFoundHttpException

I am trying to redirect the user to a custom 404 page in case of NotFoundHttpException in Laravel 5.4

To do so, I added following piece of code in App\Exceptions\Handler.php file:

public function render($request, Exception $exception)
{
    if ($exception instanceof NotFoundHttpException) {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Not Found'], 404);
        }
        return response()->view('404', [], 404);
    }
    return parent::render($request, $exception);
}

For some reason this doesn't work. Can someone please point if I am doing something wrong here?

Thanks!

like image 310
whitecollar Avatar asked Mar 09 '17 01:03

whitecollar


Video Answer


1 Answers

Laravel will render your error pages before falling back on the default view.

If your view is created in: resources/views/errors/404.blade.php. This page will be rendered before the fallback error page.

The views in the above directory should have the names of the HTTP status code which you are trying to render.

More information can be found in the laravel docs:

https://laravel.com/docs/5.4/errors#custom-http-error-pages

like image 59
Josh Bolton Avatar answered Oct 14 '22 11:10

Josh Bolton