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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With