Laravel 9 Middleware Example Tutorial


Share:
 | Published By: Admin | Published On: May 09, 2022 | Category: Laravel 9

Hi Guys,

Today, I will explain to you how to use Middleware in laravel 9. so you can just follow my step by step and learn laravel 9 custom middleware example tutorial.

Simply laravel middleware filter all the HTTP request in laravel predicated projects. For example when the utilizer is do any request at that time middleware check utilizer is logged in or not and redirect accordingly. Any utilizer is not loggedin but he want to access to the dashboard or other things in projects that time middleware filter request redirect to the utilizer.

In this article we will give a define example of active or inactive users. after we call middleware in routes to restrict logged user to access that routes, Let's check it.

So let's start to the example and follow the my all step.

Step 1: Download Laravel

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app
Step 2: Create Middleware

In this first step first of all we will create a middleware in app/http/middleware simple command so let's your open command prompt and paste bellow code.

php artisan make:middleware UserStatus
Step 3: Register Middleware

Next, a step we will go to app/http/kernel.php and register your middleware custom name.

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 = [
        ....
        'UserStatus' => \App\Http\Middleware\UserStatus::class,
    ];
}
Step 4: Implement Your Logic In Middleware

In this third step we will, go to app/http/middleware and implement your logic here :

app/Http/Middleware/UserStatus.php
<?php
 
namespace App\Http\Middleware;
use Closure;
 
class UserStatus
{
    /**
     * 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 5: Create Route

In this fourth step to create a route and use custom middleware here in web.php file and use this code.

routes/web.php
<?php

use App\Http\Controllers\StudentController;
use App\Http\Middleware\UserStatus;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::middleware([UserStatus::class])->group(function(){
    Route::get('home', [StudentController::class,'home']);
});
Step 6: Add Method In StudentController

Now we will create one method name language and let’s check :

app/Http/Controllers/StudentController.php
<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;

class StudentController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function home()
    {
        dd('You are active');
    }
}

Now, We have successfully engender custom middleware in laravel 9 based project with a simple example.you can check and run the example.

Run Laravel App:

All steps have been done, now you have to type the given command and hit enter to run the laravel app:

php artisan serve

Now, you have to open web browser, type the given URL and view the app output:

http://localhost:8000/home

I Hope it Will Help You...

Share: