Laravel 8 Middleware Example Tutorial

Laravel 8 Middleware Example Tutorial

Laravel 8 middleware tutorial. In this tutorial, you will learn how to create middleware and how it use in laravel 8 based app.

Note that, Simply laravel middleware filter all the http request in laravel based projects. For example when user is do any request that time middleware check user is loggedin or not and redirect accordingly. Any user is not loggedin but he want to access to dashboard or other things in projects that time middleware filter request redirect to user.

Now, we will give an example of active or inactive users access laravel 8 app or not. So, add this middleware with routes to restrict logged user to access routes, if he/she is inactive by admin.

Laravel 8 Middleware Tutorial

Step 1: Create Middleware

In this step, open terminal and execute the following command to create custom middleware in laravel 8 app. So let’s open your command prompt and execute below command on it:

php artisan make:middleware CheckStatus

After successfully create middleware, 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,
    ];
}

After successfully register your middleware in laravel project, 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 2: Add Route

In this step, simply create a route and use custom middleware with routes for filter every http request:

 
 //routes/web.php 
 
use App\Http\Controllers\HomeController;
use App\Http\Middleware\CheckStatus;

Route::middleware([CheckStatus::class])->group(function(){

Route::get('home', [HomeController::class,'home']);

});

Step 3: Add Method in Controller

In this step, create one method name home and add this method on HomeController.php file, which is placed on app/Http/Controllers/ directory:

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

Conclusion

Laravel 8 middleware tutorial, You have learned how to create custom middleware in laravel 8 based project and as well as how to use it.

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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