Laravel 8 Custom Middleware Tutorial Example

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share
Laravel 8 Custom Middleware

Hello Dev’s, Now in this tutorial I will create a custom middleware in laravel 8 application. From here we will learn about how to create a custom middleware in laravel 8 application and we know how to use middleware in laravel 8 based project. I will Simply create one custom middleware and check language in query string in laravel 8 application. Here we know about Laravel 8 Custom Middleware.

So now simply laravel middleware filter all the http request in any laravel based Application. For a good example when a user is do any request that time middleware check that user is login or not and redirect accordingly. If any user is not login but they are want to access to dashboard or other pages in those projects that time middleware filter request redirect to the user.

So now in this laravel 8 middleware tutorial, i will give you a short and simple example of active or inactive users. So now i will cal al middleware in routes to restrict logged user to access that routes, and also to check that users is blocked or not by admin.

Step:1 Create Middleware

So in this step, We have to create a new custom middleware in laravel based project. So let’s open your command prompt and run below command :

php artisan make:middleware CheckStatus

Step:2 Register Middleware

Now after successfully create a new middleware, so go to app/http/kernel.php and register your custom middleware here :

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

Step:3 Implement logic In Middleware

Now after successfully register our middleware in laravel project, so go to app/http/middleware and implement your logic here :

app/Http/Middleware/CheckStatus.php

<?php
 
namespace App\Http\Middleware;
 
use Closure;
 
class CheckStatus
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (auth()->user()->status == 'active') {
            return $next($request);
        }
            return response()->json('Your account is inactive');
 
    }
}
?>

Step:4 Create Route

Now in this step, it’s simply we will create a new route and also use custom middleware here. So Filter http every request and protect routes :

 //routes/web.php 
 
 Route::middleware(['checkStatus'])->group(function(){
 
 Route::get('home', 'HomeController@home');
 
 });

Step:5 Add Method in Controller

So now we will create one method name language and let’s start and check :

app/Http/Controllers/HomeController.php

<?php
 
 namespace App\Http\Controllers;
 
 use Illuminate\Http\Request;
 class HomeController extends Controller
 {
     public function home()
     {
         dd('You are active');
     }
 }
?>

Now we have successfully create custom middleware in laravel 8 based project with a simple example. So run the example.

Read Also: Laravel 8 Mobile Number Verification Tutorial

Thanks for read. I hope it help you. For more you can follow us on facebook

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →