Wednesday, March 8, 2017

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!



via Rishab

Advertisement