Concept of Group Middleware in Laravel 8 Tutorial

Share this Article
Reading Time: 5 minutes
8,703 Views

Middleware are just like the filter funnel which filters the HTTP request inside any application. Any request before enter into application must needs to pass through the barrier of middleware which application contains.

Middlewares basically used to filter the authenticated request in application. By the help of which 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 –

Inside this article, we will see the concept of Group Middleware in Laravel 8.

Concept of Group Middleware in Lara...
Concept of Group Middleware in Laravel 8 Tutorial

Let’s get started.


What is Group Middleware?

Group middleware name itself clears that, the middleware which will be applied to a group of routes in application. All the group routes where we apply the concept of Group middleware inside application must be needs to go with filter funnel i.e middleware and then process next.

By the help of 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 application, Kernel.php file will be used which is inside /app/Http. Kernel.php, it’s a class file which contains registration of middlewares.


Laravel Installation

We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.

Here is the command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

To start the development server of Laravel –

$ php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.


Example – Group Middleware Implementation

Let’s take an example to understand the concept of group middleware inside laravel application.

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

Create a Middleware

Open project into terminal and run this artisan command.

$ php artisan make:middleware CountryCheck

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

Open CountryCheck.php file and write this 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);
    }
}
  • $request->country is NOT (US, IN, AFG), it will open no access page.

Register Middleware in Application

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

protected $middlewareGroups = [
   ...

   'apprestrict' => [ 
       \App\Http\Middleware\CountryCheck::class,
    ],

   ...
];

apprestrict is the name of group middleware.

Create NoAccess Route

Open web.php from /routes and write this route into it.

//.. Other routes

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

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

// Protected group by middleware
Route::group(["middleware" => ["apprestrict"]], function(){

   Route::get("route-2", function(){ echo "<h3>Welcome To Route 2</h3>"; });
  
   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 at /resources/views folder. Open write this simple code into it.

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

Application Testing

Open project to terminal and type the command to start 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-1/?country=uk

Welcome To Route 1

Protected Routes

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.

We hope this article helped you to learn about Concept of Group Middleware in Laravel 8 Tutorial in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5