中间件参数

时间:2015-12-23 09:14:20

标签: laravel laravel-5.1

From the docs我可以看到将参数editor传递给您编写的中间件role

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

要在中间件中使用该参数,只需将参数传递给句柄函数

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

我的问题是,这个editor参数来自哪里?我只能看到传递的URL字符串中的id

2 个答案:

答案 0 :(得分:4)

handle参数中的$role变量将包含role:之后传入的变量

因此role:editor将返回"editor"

答案 1 :(得分:2)

您必须设置角色参数。 editor可以是adminusermoderator等,然后将此值传递给handle方法。有关更多详细信息,请参阅以下链接:https://mattstauffer.co/blog/passing-parameters-to-middleware-in-laravel-5.1