How to Create and Register Middleware in Laravel 11?

How to Create and Register Middleware in Laravel 11

Middleware in Laravel acts as a bridge between request and response. Middleware are used for various purposes like authentication, logging, security and much more.

Laravel Development Services

In this blog, we will learn the process of creating and registering middleware in Laravel 11.  

Steps to Create and Register Middleware in Laravel 11:

In Laravel 11, we show no kernel.php file and the Middleware directory is not present.

Here is the step-by-step guide for it.

Let’s take one example for it.

Step 1: Define/Create a Middleware

We can create a middleware by using this command 

$ php artisan make:middleware EnsureTokenIsValid

This command will create a new middleware EnsureTokenIsValid at the app/Http/Middleware directory.

Now replace this file with the following code. The code works like if the input token does not have the same value as my-secret-token; then it will redirect to the home page.

app/Http/Middleware/EnsureTokenIsValid.php

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class EnsureTokenIsValid
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        if ($request->input('token') !== 'my-secret-token') {
            return redirect('/home');
        }

        return $next($request);
    }
}

Step 2: Register alias for the middleware 

Now, we can register this middleware with proper alias. Open the bootstrap/app.php file and write the below code.

bootstrap/app.php
//....
->withMiddleware(function (Middleware $middleware) {
        $middleware->alias([
            "isValidToken" => App\Http\Middleware\EnsureTokenIsValid::class
        ]);
})
// other stuff do

Step 3: Use middleware

Now, we can use this middleware in web.php.

Use 1: 

use App\Http\Middleware\EnsureTokenIsValid;
 
Route::get('/profile', function () {
    // ...
})->middleware(EnsureTokenIsValid::class);

Use 2:

use App\Http\Middleware\EnsureTokenIsValid;
 
Route::get('/profile', function () {
    // ...
})->middleware('isValidToken');

Conclusion:

By following this guide, you can easily create and register middleware for your Laravel application.

Hire Laravel Developer

If you face difficulty in creating or registering middleware for your Laravel application, let me know through the comment section.

Happy Reading!

Contact Us
Previous Article

How to Use Multiple Database in Laravel 11?

Next Article

Magento 2: How to Use Dynamic Row with UI Components

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨