How To Work with Global Middleware in Laravel 10 Tutorial

Reading Time: 5 minutes
336 Views

Inside this article we will see the concept i.e How To Work with Global Middleware in Laravel 10 Tutorial. Article contains the classified information i.e What is Global Middleware & How to use it in Laravel 10 application.

In Laravel, Middleware is a mechanism that allows developers to filter HTTP requests coming into their application. Global Middleware, specifically, refers to a set of Middleware that is applied to every HTTP request that enters your Laravel application.

In laravel we have three types of middlewares –

  • Global Middleware
  • Group Middleware
  • Route Middleware

Inside this article, we will see the concept of Global Middleware in Laravel 10 application.

Read More: Laravel 10 Scout How To Add Full Text Search in Table

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

What is Global Middleware?

Global Middleware allows developers to perform common tasks such as authentication, session management, and input validation on every request, without having to manually apply the Middleware to every route in their application.

By using Global Middleware, developers can ensure that every request to their application is handled consistently and securely.

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.

Read More: Laravel 10 YajraBox Server Side Datatable Tutorial

Example – Global Middleware Implementation

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

  • Create a Middleware which checks country, when we open URL.
  • 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 inside /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);
    }
}

If request $request->country is NOT in 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 $middleware and add this.

protected $middleware = [
    //...

    \App\Http\Middleware\CountryCheck::class,
    
    //...
];

Read More: Laravel 10 Step By Step Stub Customization Tutorial

Create NoAccess Route

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

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

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

Create NoAccess Page

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

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

Application Testing

Run this command into project terminal to start development server,

php artisan serve

URL: http://127.0.0.1:8000/?country=us

*** We can access the routes. ***

URL – http://127.0.0.1:8000/?country=uk

Output:

Sorry! You have no access to open this page.

We hope this article helped you to learn about How To Work with Global Middleware in Laravel 10 Tutorial in a very detailed way.

Read More: How To Work with Laravel 10 Model Events Tutorial

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

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.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more