ItSolutionStuff.com

How to Define Fallback Routes in Laravel?

By Hardik Savani • November 5, 2023
Laravel

Hello Dev,

This article will provide an example of laravel fallback route. you can understand a concept of how to use fallback route in laravel. we will help you to give an example of laravel define fallback route. In this article, we will implement a laravel fallback no other route matched.

What is fallback route in laravel?, you may define a route that will be executed when no other route matches the incoming request. So, basically, it's handle 404 error page. you can create your own 404 error using fallback route.

so, let's see how to define fallback route in laravel step by step.

Step 1: Install Laravel App

This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Create Fallback Route

Furthermore, open routes/web.php file and add fallback route with controller name as like the below.

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\FallbackController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

Route::fallback(FallbackController::class);

Step 3: Create Controller

In this step, we will create a new FallbackController; in this file, we will have a __invoke method to call view file.

next, let's update the following code to Controller File.

app/Http/Controllers/FallbackController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FallbackController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function __invoke()

{

return view('fallback');

}

}

Step 4: Create Blade File

Here, we will create fallback.blade.php file with html code. so let's update as like the below:

resources/views/fallback.blade.php

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">

<title></title>

</head>

<body>

<div class="container">

<h1>Call Fallback Route Once a Route Doesn't Exist.</h1>

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod

tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,

quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo

consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse

cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non

proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

</div>

</body>

</html>

Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/demo-demo

Output:

I hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Generate a Dropdown List of Timezone in Laravel?

Read Now →

How to use Bootstrap Icons in Laravel Vite?

Read Now →

How to Install Sweetalert2 in Laravel 10 Vite?

Read Now →

Laravel Country List with Flags Example

Read Now →

How to Show Data in Modal using Ajax in Laravel?

Read Now →

Laravel 10 Clear Cache of Route, View, Config, Event Commands

Read Now →

Laravel Route Pass Multiple Parameters Example

Read Now →

How to Get All Routes in Laravel?

Read Now →

Laravel Redirect to Route from Controller Example

Read Now →

Laravel Vue Router Example From Scratch

Read Now →

Laravel Get Route Parameters in Middleware Example

Read Now →

How to Get Current Route Name in Laravel?

Read Now →

How to Redirect Route with Querystring in Laravel?

Read Now →

How to Generate Route with Query String in Laravel?

Read Now →