DEV Community

loading...
Cover image for How To Use/Create Middleware Laravel 8 Example

How To Use/Create Middleware Laravel 8 Example

سامية🌻
nerdy/CS student/laravel lover <3/full-stuck dev/
・2 min read

Today In this tutorial, you will learn how to create middleware and how it use in laravel 8 applications.

Simply laravel middleware filter all the HTTP requests in laravel based on projects. For example when the user is doing any request that time middleware check user is logged in or not and redirect accordingly. Any user is not logged in but he wants to access the dashboard or other things in projects that time middleware filter requests redirect to the user.

This 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.
Step1 :Create Middleware:
Open the terminal and execute the following command to create custom middleware in laravel 8. So let’s open your command prompt and execute below command on it to learn PHP middleware:
Step 1: Create Middleware
Open the terminal and execute the following command to create custom middleware in laravel 8. So let’s open your command prompt and execute below command on it to learn PHP middleware:

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,
    ];
}
Enter fullscreen mode Exit fullscreen mode

step 2: Implement Logic In Middleware:
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');

    }
}
?>
Enter fullscreen mode Exit fullscreen mode

step 3:Add Route
Simply create a laravel 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']);
Enter fullscreen mode Exit fullscreen mode

Step 4: Add Method In Controller
Create one method name home and add this method on HomeController.php file, which is placed on **app/Http/Controllers/ **directory We lare learn the middleware tutorial:

<?php

 namespace App\Http\Controllers;

 use Illuminate\Http\Request;
 class HomeController extends Controller
 {
     public function home()
     {
         dd('You are active');
     }
 }
?>
Enter fullscreen mode Exit fullscreen mode

Discussion (0)