Laravel 9 Middleware Example Tutorial

Laravel 9 Middleware Example Tutorial

Laravel 9 middleware example; In this tutorial, we will learn how to create and how to use middleware in laravel 9 apps.

Laravel middleware filters all the HTTP requests in laravel based 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 to the dashboard or other things in projects that time middleware filter request redirects to the user.

Laravel 9 Middleware Example Tutorial

Follow the following steps to create and use middleware in laravel 9 apps:

  • Step 1 – Create Middleware
  • Step 2 – Register Middleware
  • Step 3 – Implement Logic Into Your Middleware File
  • Step 4 – Add Route
  • Step 5 – Add Method in Controller

Step 1 – Create Middleware

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

php artisan make:middleware CheckStatus

Step 2 – Register Middleware

Register 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 Into Your Middleware File

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 4 – 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 5 – 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 9 middleware tutorial, we have learned how to create custom middleware in laravel based project and as well as how to use it.

Recommended Laravel Tutorials

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, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL 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 *