Understanding Fallback Route in Laravel with Example

Published on March 16, 2020 24 sec read

Using the Route::fallback method, we may define a route that will be executed when no other route matches the incoming request. We need to add the fallback route at the end of all routes.

Fallback route overrides the default 404 page. We’re able to add more custom functionalities too.

Structure

The structure of the fallback route is:

Route::fallback(function () {
    // your logic here
});

Example

Let’s see an example:

// your other routes

// fallback route
Route::fallback(function() {
    return "You're message goes here!";
});

Now if user enters random URL like https://example.com/hstuwks, the user will see the fallback route’s response.

For this example, the user will see:

You're message goes here!
That’s all. Thank you. 🙂

Author

Hey, I'm Md Obydullah. I build open-source projects and write about Laravel, Linux server, modern JavaScript and more on web development. If you enjoy my content, please consider supporting what I do!

Buy me a coffeeBuy me a coffee Follow

Leave a Reply

Your email address will not be published. Required fields are marked *