Understanding Fallback Route in Laravel with Example

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.


Software Engineer | Ethical Hacker & Cybersecurity...

Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.