Laravel has added support for middleware parameter since version 5.1 . So it is nice that we can pass middleware parameter from Routes.php or from controller. Let’s see an example.
Route::get('test',['middleware'=>'hasAccess:Admin', function(){ return "Access granted for this operation"; }]);
or in controller constructor
//Other stuff in controller public function __construct() { $this->middleware('hasAccess:admin'); } //Other stuff in controller
and in middleware handle function you can catch argument which is passed as mentioned below:
public function handle($request, Closure $next, $roleName) { if($roleName=="Admin") return $next($request); else return redirect()->guest(route('login')); }
You can see that our route is protected with admin role. Means user with admin role can only access test route in our application.
But what if i have to check two roles or three roles. Should i have to catch three variables in handle method of hasAccess middleware. You can but there is a better way . As PHP 5.6 released they provide variable function argument feature so you can use it to pass dynamic argument and treat it as array.
See below code example to demonstrate passing array in middleware as argument.
Route::get('/admin',['middleware'=>'hasAccess:Admin,Registered', function () { return "Access is granted for this operation"; }]);
and in Middleware handle function
public function handle($request, Closure $next, ...$params) { print_r($params); exit; return $next($request); }
You will get below output.
Array ( [0] => Admin [1] => Registered )
So in this way you can pass array as middle ware parameter.
Reblogged this on AdhocMaster and commented:
Great functionality!
LikeLiked by 1 person
Very very helpful. Thank you for posting this.
LikeLike
Welcome!
LikeLike
Nice, I’m already using in my projects.
Thanks!
LikeLike
I’m already using in my projects.
Thanks!
LikeLike
Earlier when 5.1 has not introduce this feature, i was doing this by using session, but you know it did not look clean as it is now.
LikeLike
I have this error
exception ‘Symfony\Component\Debug\Exception\FatalErrorException’ with message ‘syntax error, unexpected ‘.’, expecting ‘&’ or variable (T_VARIABLE)’
LikeLike
Which php version are you using? It seems that you are using php version < 5.6, i strictly write that variable function argument feature is available only in php 5.6+ .
For your reference please refer this link http://stackoverflow.com/questions/18427844/syntax-error-unexpected-expecting
LikeLike
Can’t seem to get it working when using middleware parameters in the controller – any ideas? http://stackoverflow.com/questions/32606970/laravel-middleware-parameters-from-controller-constructor
LikeLike
Sent reply on stackoveflow.
LikeLike
Thanks You,
Ahesan bhai,
I was looking for passing multiple parameters in laravel middleware, So I found by reading your post and set three dots[…] before of parameter in middleware and it worked. Really good work…keep it up
LikeLike
Welcome. Actually this is not laravel feature but core php feature.
LikeLike
it’s broken in php 5.5. is there any work around?
LikeLike
This variadic function argument is not php 5.5 feature, it’s in php 5.6. See http://docs.php.net/manual/en/migration56.new-features.php#migration56.new-features.variadics
LikeLike
Nice. Thanks for article
LikeLike
Thank you so much.
LikeLike