Posts Learn Snippets Categories Tags About
/

How to Define Fallback Routes in Laravel 8

Learn how to define fallback routes in Laravel and provide a user with a better page when no route matches the incoming request.

Created on Jun 30, 2021

8 views

In Laravel, you can make use of "Route::fallback" method to catch any incoming request that matches no route. Since typically any unhandled requests will automatically render the "404" page, having a fallback route inside the routes/web.php allow you to customize and return a custom route.

You can define the fallback route like below. The content of the view is up to your customization.
#routes/web.php

Route::fallback(function () {
  $message = 'You have hit a fallback page!';

  return view('page.fallback-page', ['message' => $message]);
});

Created by

PostSrc poster

PostSrc

Full stack developer mainly focusing on the front end side of the web. The main author of PostSrc and loves writing tutorial and guides online.