Thursday, 4 May 2023

How to pass Middleware Parameters in laravel

 => " Middleware can also receive additional parameters. "


=> " For example, if your application needs to verify that the 

   authenticated user has a given "role" before performing a given action, "

   

=> "Additional middleware parameters will be passed to the middleware after the $next argument:"

   

=> Middleware parameters may be specified when defining the route by 

   separating the middleware name and parameters with a :.

  

   Multiple parameters should be delimited by

 commas:


=> in route


Route::put('/post/{id}', function ($id) {

    //

})->middleware('role:editor');



=> in Middleware


<?php

 

namespace App\Http\Middleware;

 

use Closure;

 

class EnsureUserHasRole

{

    public function handle($request, Closure $next, $role)

    {

        if (! $request->user()->hasRole($role)) {

            // Redirect...

        }

 

        return $next($request);

    }

 }

No comments:

Post a Comment

Other post

1) The create() Method The  create()  method is a powerful and convenient way to store data in your Laravel application. It is typically use...