How To Create Middleware with Parameters In Larave 8

Nov 15, 2021 . Admin



Hi Guys,

We are know that laravel is the best framework in PHP. Laravel framework provide us the several functionality and you can also find from this site also. But now here in this post you can learn about how to create a custom middleware with passing parameters in laravel application example and also know how to use middleware with route parameters in your laravel application . Now in this example we will about learn how to add middlware with check the user have access role for this route from the scratch in our laravel application.

So here I will also add how to create a middleware in my previous post, you can see that also.

Now in this example i will add middleware for the check user have role access or not for this route.

So at first need to create RoleMiddleware middleware by using bellow command:

Step 1 : Create Middleware
php artisan make:middleware RoleMiddleware
Step 2 : Register Middleware

Now you have to register and create the aliase above the middleware in Kernel.php file. So at first open Kernel.php and add the bellow line.

app/Http/Kernel.php
<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    protected $routeMiddleware = [
        'role' => \App\Http\Middleware\RoleMiddleware::class,
    ];
}
Step 3 : Implement logic In Middleware

Okay , now we can found the RoleMiddleware.php in location app/Http/Middleware directory and need to open RoleMiddleware.php file and then put the bellow code on that file. So in this file i will check given parameter role is access for the current login user or not.

app/Http/Middleware/RoleMiddleware.php
<?php

namespace App\Http\Middleware;

use Closure;
use Auth;

class RoleMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next, $role)
    {
    	if (! $request->user()->hasRole($role)) {
          return redirect()->route('home');
      }
        return $next($request);
    }
}

Now you have to register and create the aliase above the middleware in Kernel.php file. So at first open Kernel.php and add the bellow line.

Step 4 : Create Route

Hope now we are ready to use role middleware in our routes.php file. So we can see here how to use middleware in routes.php file.

app/Http/routes.php
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;

Route::get('home',[HomeController::class,'index'])->name('home');
Route::group(['middleware' => 'role:admin'], function () {     
    Route::get('admins', [HomeController::class,'admins'])->name('admins'); 
});
OR
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;

Route::get('home', [HomeController::class, 'index'])->name('home');
Route::get('admins', [HomeController::class, 'admins'])->name('admins')->middleware('role');

I hope it can help you...

#Laravel 8 #Laravel