I am trying to figure out to show 404 page not found if a route is not found. I followed many tutorials, but it doesn’t work.
I have 404.blade.php
in laravelresourcesviewserrors
Also in handler.php
public function render($request, Exception $e) { if ($e instanceof TokenMismatchException) { // redirect to form an example of how i handle mine return redirect($request->fullUrl())->with( 'csrf_error', "Opps! Seems you couldn't submit form for a longtime. Please try again" ); } /*if ($e instanceof CustomException) { return response()->view('errors.404', [], 500); }*/ if ($e instanceof SymfonyComponentHttpKernelExceptionNotFoundHttpException) return response(view('error.404'), 404); return parent::render($request, $e); }
If I enter wrong URL in browser, it returns a blank page. I have
'debug' => env('APP_DEBUG', true),
in app.php.
Can anyone help me how to show a 404 page if route is not found? Thank you.
Advertisement
Answer
I recieved 500 errors instead of 404 errors. I solved the problem like this:
In the app/Exceptions/Handler.php file, there is a render function.
Replace the function with this function:
public function render($request, Exception $e) { if ($this->isHttpException($e)) { switch ($e->getStatusCode()) { // not authorized case '403': return Response::view('errors.403',array(),403); break; // not found case '404': return Response::view('errors.404',array(),404); break; // internal error case '500': return Response::view('errors.500',array(),500); break; default: return $this->renderHttpException($e); break; } } else { return parent::render($request, $e); } }
You can then use views that you save in views/errors/404.blade.php, and so on.