将数组传递给 Laravel 中的中间件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43685610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass an array to a middleware in laravel
提问by Jorge Ivan Aguirre
I got this handle in a middleware called rolMiddleware:
我在一个叫做 rolMiddleware 的中间件中得到了这个句柄:
public function handle($request, Closure $next, $roles)
{
//dd($request->user());
foreach ($roles as $rol) {
if ($request->user()->getTipoUsuario($request->user()->tipo_usuario_id)->getNombreTipoUsuario() == $rol) {
return $next($request);
}
}
abort(403, "?No hay autorizacion!");
}
But $roles is an array, here is the route where I use the middleware:
但是 $roles 是一个数组,这里是我使用中间件的路线:
Route::get('/mid', ['middleware' => 'roles:super admin', function () {
return "done";
}]);
and the error that gives me is:
给我的错误是:
ErrorException in RolMiddleware.php line 22:
Invalid argument supplied for foreach()
You may thing that I do not need an array because I am only using it in super admin, for that route I only need super admin, but there would be routes that can be for super admin and the admin of an area.
您可能会认为我不需要数组,因为我只在超级管理员中使用它,对于该路由,我只需要超级管理员,但是会有可以用于超级管理员和区域管理员的路由。
回答by hassan
In laravel , you can separate your parameters which you want to pass to middleware using comma ,
as follows:
在 laravel 中,您可以使用逗号分隔要传递给中间件的参数,,
如下所示:
Route::get('/mid', ['middleware' => 'roles:super,admin', function () {
// ^ note this
return "done";
}]);
note that, this won't send parameters as an array, so you can't loop over $roles
unless you use your passed parameters as ellipsis parametersas follows :
请注意,这不会将参数作为数组发送,因此$roles
除非您使用传递的参数作为省略号参数,否则您无法循环,如下所示:
public function handle($request, Closure $next, ...$roles)
rather, you will need to use a single parameter for each role:
相反,您需要为每个角色使用一个参数:
public function handle($request, Closure $next, $role1, $role2) // .... and so on
回答by Markinson
I don't exactly understand what your functions do, but you can try something like this:
我不完全明白你的功能是做什么的,但你可以尝试这样的事情:
public function handle($request, Closure $next, $roles)
{
if(is_array($roles)){
//dd($request->user());
foreach ($roles as $rol) {
if ($request->user()->getTipoUsuario($request->user()->tipo_usuario_id)->getNombreTipoUsuario() == $rol) {
return $next($request);
}
}
}else{
if($request->user()->getTipoUsuario($request->user()->tipo_usuario_id)->getNombreTipoUsuario() == $roles)
return $next($request);
}
abort(403, "?No hay autorizacion!");
}
回答by venkatskpi
Route:
路线:
Route::get('/access', ['middleware' => 'hasroles:super,admin', function () {
}]);
passing one parameter to check user have created permission in my cause
传递一个参数来检查用户是否在我的事业中创建了权限
Route::middleware('admin')->namespace('Admin')->prefix('admin')->group(function(){
Route::get('/home', 'MainController@getIndex')->name('admin.index')->middleware("hasrole:create");
Middleware:
中间件:
1.Using Parameters
1.使用参数
public function handle($request, Closure $next, $parm1, $parm2){}
2.Using var-arg
2.使用可变参数
public function handle($request, Closure $next, $parm1, $parm2){}
public function handle($request, Closure $next, ...$parm1){}
Two-way Middleware Use
双向中间件使用
1: Register routeMiddleware
1:注册路由中间件
// Within App\Http\Kernel Class...
protected $routeMiddleware = [
'hasrole' => \Illuminate\Auth\Middleware\HasRole::class,
Using:
使用:
Route::get('admin/profile', function () {
})->middleware('hasrole');
2: Not Register in routeMiddleware
2:没有在routeMiddleware中注册
Using:
使用:
use App\Http\Middleware\HasRole;
Route::get('admin/profile', function () {
//
})->middleware(HasRole::class);