What Is The Use Of Fallback Routes In Laravel Framework

admin_img Posted By Bajarangi soft , Posted On 17-09-2020

Route::fallback method define a route that will be executed when no other route matches the incoming request or when you past a wrong path name. We need to add the fallback route at the end of all routes.

What Is The Use Of Fallback Routes In Laravel Framework


Fallback Routes

Using the Route::fallback method, you may define a route that will be executed when no other route matches the incoming request.

Typically, unhandled requests will automatically render a "404" page via your application's exception handler.

However, since you may define the fallback route within your routes/web.php file, all middleware in the web middleware group will apply to the route. You are free to add additional middleware to this route as needed:

 

Route::fallback(function () {
    //
});
 

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.

Example(1)

Create new route in web.php

Route::fallback(function() {
    return "<h1>Something missed in your path</h1>";
});


Now if user enters random URL like http://localhost/laraveldemoproject/public/hvghvg, the user will see the fallback route’s response.

Related Post