@dana
To show a 404 page if a route is not found in Laravel, you can follow these steps:
1 2 3 |
if ($exception instanceof SymfonyComponentHttpKernelExceptionNotFoundHttpException) { return response()->view('errors.404', [], 404); } |
This code checks if the exception is an instance of NotFoundHttpException
class, which occurs when a route is not found. If it is, it returns a view named errors.404
with a 404 status code.
Now, whenever a route is not found, Laravel will automatically display the 404.blade.php
view, showing a 404 page to the user.