myol
myol

Reputation: 9828

Middleware paramaters

From the docs I can see that to pass a parameter editor to a middleware role you write

Route::put('post/{id}', ['middleware' => 'role:editor', function ($id) {
    ...
}]);

And to use the parameter in the middleware, you simply pass the parameter into the handle function

public function handle($request, Closure $next, $role)
{
    ...
}

My question is, where is this editor parameter coming from? I can only see the id in the URL string being passed about.

Upvotes: 0

Views: 51

Answers (2)

Mirceac21
Mirceac21

Reputation: 1754

You have to set the role parameter. editor can be admin, user, moderator etc, then this value is passed to handle method. For more detailed information see this link: https://mattstauffer.co/blog/passing-parameters-to-middleware-in-laravel-5.1

Upvotes: 2

Joost
Joost

Reputation: 761

The $role variable within the handle parameters will contain the variable passed in after role:

so role:editor will return "editor"

Upvotes: 4

Related Questions