Friday , November 15 2024

Route Middleware

What is Middleware?

Inside this article, we will see the concept of Route Middleware in Laravel 9.

Middleware is just like the filter funnel which filters the HTTP request inside any application. Any request before entering into the application must need to pass through the barrier of middleware that the application contains.

Middlewares basically used to filter the authenticated request in the application. With the help of this, we can detect which route is valid or invalid. Apart from this, we have several other examples.

In Laravel, we have three types of middlewares –

Assuming Laravel is already installed inside your system.

What is Route Middleware?

Route middleware name itself clears that, the middleware will be applied to a specific route in the application. The route where we apply the concept of Route middleware inside the application must need to go with filter funnel i.e middleware and then process next.

With the help of the PHP artisan command, we create Middleware in Laravel.

php artisan make:middleware <MiddlewareName>

Middlewares will be stored inside /app/Http/Middleware. To register middleware inside the application, Kernel.php file will be used which is inside /app/Http. Kernel.php, it’s a class file that contains the registration of middlewares.

Example – Route Middleware Implementation

Let’s take an example to understand the concept of route middleware inside Laravel application.

  • Create a Middleware that checks the country, when we open the URL.
  • Example We will have 3 routes in which a route should be protected by middleware and the other two routes will be open to access for everyone.
  • US, IN, AFG should be allowed inside applications, they can access application routes.
  • UK, AUS should be restricted to use or access route.

Create a Middleware

Open the project into the terminal and run this artisan command.

php artisan make:middleware CountryCheck

It will create a file with the name CountryCheck.php inside /app/Http/Middleware folder.

Open CountryCheck.php file and write the following code into it.

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class CountryCheck
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        if ($request->country && !in_array($request->country, array("us", "in", "afg"))) {
            return redirect("noaccess");
        }
        return $next($request);
    }
}

If request $request->country is NOT in the list of these (US, IN, AFG) then it will open no access page.

Register Middleware in Application

Open Kernel.php from /app/Http. Search for $routeMiddleware and add this.

protected $routeMiddleware = [
    //...

    'apprestrict' => \App\Http\Middleware\CountryCheck::class,
    
    //...
];

apprestrict is the name of route middleware.

Create NoAccess & Protected Routes

Open web.php from /routes and add these routes to it.

//...

Route::view("noaccess", "noaccess");

Route::get("route-1", function(){ 
  echo "<h3>Welcome To Route 1</h3>"; 
});

// Protected by middleware
Route::get("route-2", function(){ 
  echo "<h3>Welcome To Route 2</h3>"; 
})->middleware("apprestrict");
  
Route::get("route-3", function(){ 
  echo "<h3>Welcome To Route 3</h3>"; 
});

Next, we need to create noaccess.blade.php file.

Create NoAccess Page

Create a file called noaccess.blade.php inside /resources/views folder. Open the file and write this simple code into it.

<h3>Sorry! You have no access to open this page.</h3>

Application Testing

Open the project to the terminal and type the command to start the development server.

php artisan serve
Normal Routes

URL – http://127.0.0.1:8000/route-1/?country=us

*** Welcome To Route 1 ***

URL – http://127.0.0.1:8000/route-3/?country=uk

*** Welcome To Route 3 ***
Protected Route

URL – http://127.0.0.1:8000/route-2/?country=us

*** Welcome To Route 2 ***

URL – http://127.0.0.1:8000/route-2/?country=uk

Output:
Sorry! You have no access to open this page.

Check Also

How Routing Is Done In Laravel

What is Routing in Laravel? Routing simply means mapping application URLs to application view files. …