Laravel 9 Custom Middleware Example Tutorial

Hello artisan,

In this Laravel 9 middleware tutorial, I will show you how to create a custom middleware in Laravel and what is middleware actually, and how it works. This laravel middleware will give you an example of laravel 9 middleware example.

I want to explain step by step laravel 9 custom middleware example. We will see how to create custom middleware in laravel 9. I will help you to show you an example of how to use middleware in laravel 9. Laravel middleware is used to filter HTTP requests in web applications before showing the server result. There are many predefined middlewares in Laravel Laravel like guest, auth, can, etc.

In this laravel middleware example, I will create a custom middleware that will allow active users to access pages. Let's see the example:

 

Step 1: Install Laravel

This step is optional if you have a laravel project already. If not then execute the below command:

composer create-project laravel/laravel example-app

 

Step 2: Create Middleware

In this step, open the terminal and run the below command to create a custom middleware file, so let's run the below command:

php artisan make:middleware IsActive

 

Now, it's created a new IsActive.php file. let's update the following code on this file.

app/Http/Middleware/IsActive.php

namespace App\Http\Middleware;
  
use Closure;
use Illuminate\Http\Request;
  
class IsActive
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        if (!auth()->user()->is_active) {
            return response()->json('Your account is inactive');
        }
  
        return $next($request);
    }
}

 

Read also: Laravel 9 Gates and Policies Tutorial with Example

 

Step 3: Register Middleware

In this step, we have to register middleware on the Kernel.php file. we will call is-active of newly created middleware. so let's update the following file.

app/Http/Kernel.php

namespace App\Http;
  
use Illuminate\Foundation\Http\Kernel as HttpKernel;
  
class Kernel extends HttpKernel
{
    ....
  
    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        ....
        'is-active' => \App\Http\Middleware\IsActive::class,
    ];
}

 

Read also: Why And When To Use Interface In Laravel Application

 

Step 4: Use Middleware

In this step, we will create one route and show you how to use middleware in the route file. so let's open your route file and update the following code:

routes/web.php

use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\RSSFeedController;
   
/*
|--------------------------------------------------------------------------
| 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::get('/test-user', [App\Http\Controllers\HomeController::class, 'index'])
->middleware(['is-active']);

 

Now if you visit this path /test-user, you will see, only active users can visit this page. This is the use case of middleware in the Laravel application. if you want to learn more about middleware, you can see this online video course.

 

Course Link: How Does Middleware Work in Every Framework

 

Hope it can help you.

 

#laravel #laravel-9x #laravel-middleware