Laravel - 将数组传递给@can Blade 模板
Laravel - Passing array to @can Blade template
我注册了一个应该接受整数数组的门。它的功能是 return true 如果 用户的角色 id 在传递的数组中。代码如下所示:
Gate::define('access', function ($user, $access_type) {
Log::info('-----------------------------------');
Log::info($access_type);
Log::info(gettype($access_type));
Log::info('-----------------------------------');
return in_array($user->roleid,$access_type);
});
在我的模板中,我设置了这样的限制:
@can('access',[2,4])
... html code here ...
@endcan
现在我在页面上收到这个错误:
查看日志查看变量$access_type的值和类型,貌似只得到值2.. 4缺少值。
看来我必须在匿名函数上添加另一个变量才能获得值 4。像这个:
Gate::define('access', function ($user, $access_type1, $access_type2){
... some code ...
})
当我添加新变量 $access_type2 并查看日志时,我能够看到值 4。
Now my problem is this:
- I can't manually add the variables on the anonymous function because the values being passed differs in numbers. It can receive
1 value, 2 values, 3 values, and etc. How can receive the values in
array format?
怎么样
Gate::define('access', function ($user, ...$access_types){
... some code ...
});
...$str
被称为splat operator in PHP。
此功能允许您捕获函数的可变数量的参数,如果您愿意,可以结合传入的 "normal" 个参数。
我注册了一个应该接受整数数组的门。它的功能是 return true 如果 用户的角色 id 在传递的数组中。代码如下所示:
Gate::define('access', function ($user, $access_type) {
Log::info('-----------------------------------');
Log::info($access_type);
Log::info(gettype($access_type));
Log::info('-----------------------------------');
return in_array($user->roleid,$access_type);
});
在我的模板中,我设置了这样的限制:
@can('access',[2,4])
... html code here ...
@endcan
现在我在页面上收到这个错误:
查看日志查看变量$access_type的值和类型,貌似只得到值2.. 4缺少值。
看来我必须在匿名函数上添加另一个变量才能获得值 4。像这个:
Gate::define('access', function ($user, $access_type1, $access_type2){
... some code ...
})
当我添加新变量 $access_type2 并查看日志时,我能够看到值 4。
Now my problem is this:
- I can't manually add the variables on the anonymous function because the values being passed differs in numbers. It can receive 1 value, 2 values, 3 values, and etc. How can receive the values in array format?
怎么样
Gate::define('access', function ($user, ...$access_types){
... some code ...
});
...$str
被称为splat operator in PHP。
此功能允许您捕获函数的可变数量的参数,如果您愿意,可以结合传入的 "normal" 个参数。