Fallback Route | Redirect Automatically 404 Page In Laravel

Sometimes we need to redirect user to 404 page if they hit unknown url. This redirection is a very basic and common requirement for every web application. In this tutorial  In this tutoril i will show you how to create a 404 page in laravel and will show if any url errors occurs.

Using the the fallback route Route::fallback method, you may define a route that will be executed when no other route matches the incoming request. Generally, unhandled requests will automatically render a "404" page via your application's exception handler. 

 

routes/web.php

Route::fallback(function () {

    return view("404");

});

 

Using this route in your application when unhandled requests will be happend, the user will automatically redirected to 404 page. You don't do anything more than yet. Just paste this code in your web.php file.

 

Note : The fallback route should always be the last route registered by your application.

 

Now you have to just create a custom 404.blade.php file in your views directory. Hope this laravel fallback route tutorial will help you to learm something.

Now if you want to handle all of the 404 not found errors in Laravel, just create a 404 page in your views directory.

resources/views/errors/404.blade.php

 

Then Laravel will automatically handle all of the 404 not found error. You have nothing to do any more after creating this blade file. Hope it can help you.