How To Create Custom Middleware In Laravel Using Artisan Command

  Dec 2023
  ITSolutionsGuides
  Category: Laravel
How To Create Custom Middleware In Laravel Using Artisan Command

Welcome To ITSolutionsGuides,

Lets see How To Create Custom Middleware In Laravel Using Artisan Command with it solutions guides tutorial. In Laravel projects there are lot of default middlewares like Authenticate, VerifyCsrfToken, EncryptCookies etc. If any additional middleware required we need to create the custom middleware using the artisan command and then use custom middleware in our project. The main purpose of the middleware is to filter any HTTP request based on our requirements. If we need to filter any HTTP request based on the requirements we need to create middleware using the artisan command and then apply the created middleware to the routes that need to be filtered by the middleware. Once the middleware is asigned to the routes. The middleware will check all the incoming HTTP requests form that routes. Middlewares are mainly used to create authenticated routes and also the role based routes.

Creating Middleware in Laravel 11 Now we will see How To Create a sample custom Middleware In Laravel Using Artisan Command with the example. We will create check middleware as a sample.
Learn to build Laravel middleware effortlessly with Artisan. Follow simple steps, harness the power of commands, and enhance your app's functionality.

Let's Create Middleware

Create a new project or open already exist project and then lets navigate to the root folder and the open the terminal and then execute the following command.

php artisan make:middleware Check

Let's Create Middleware

After creating the middleware our file will look as follows,


app/Http/Middleware/Check.php

<?php

namespace App\Http\Middleware;

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

class Check
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        return $next($request);
    }
}

Let's Register Middleware

We need to register our middleware in the Kernel.php file to use it. Here we will register our Check class middleware in the name check.


app/Http/Kernel.php

<?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 = [
        ....
        'check' => \App\Http\Middleware\Check::class,
    ];
}

Let's Use Middleware

Lets use our middleware in web.php file. Instead of asigning middleware to all routes seperately lets use the ->group(function () {}); to group all the routes under the middleware.

routes/web.php

Route::middleware(['check'])->group(function () {
    Route::get('/', [WebsiteController::class, 'index']);
});

We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.

Subscribe for NewsLetter

Be the first to know about releases and tutorial news and solutions.

We care about your data in our privacy policy.

ITSolutionsGuides

ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.

Contact US

ITSolutionsGuides, provide users with an easy-to-use form to reach out for support or inquiries.

whatsapp  gmail  instagram-new--v1  facebook-circled  twitter-circled  linkedin  github  pinterest 

Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides