作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有两种类型的用户,我已经创建了多个中间件。
一些路由需要允许这两种类型的用户。
我正在尝试以下代码:
Route::group(['namespace' => 'Common', 'middleware' => ['Auth1', 'Auth2']], function() {
Route::get('viewdetail', array('as' => 'viewdetail', 'uses' => 'DashboardController@viewdetail'));
});
但是它不起作用:(
最佳答案
中间件应该返回响应或将请求传递到管道。中间件相互独立,不应该知道其他中间件的运行。
您需要实现一个单独的中间件,允许 2 个角色或单个中间件,将允许的角色作为参数。
选项 1:仅创建一个中间件是 Auth1 和 Auth2 的组合版本,用于检查 2 种用户类型。这是最简单的选项,虽然不是很灵活。
选项 2:因为 5.1 版 中间件可以带参数 - 请在此处查看更多详细信息:https://laravel.com/docs/5.1/middleware#middleware-parameters .您可以实现一个单一的中间件,该中间件将获取要检查的用户角色列表,并仅在您的路由文件中定义允许的角色。下面的代码应该可以解决问题:
// define allowed roles in your routes.php
Route::group(['namespace' => 'Common', 'middleware' => 'checkUserRoles:role1,role2', function() {
//routes that should be allowed for users with role1 OR role2 go here
});
// PHP < 5.6
// create a parametrized middleware that takes allowed roles as parameters
public function handle($request, Closure $next) {
// will contain ['role1', 'role2']
$allowedRoles = array_slice(func_get_args(), 2);
// do whatever role check logic you need
}
// PHP >= 5.6
// create a parametrized middleware that takes allowed roles as parameters
public function handle($request, Closure $next, ...$roles) {
// $roles will contain ['role1', 'role2']
// do whatever role check logic you need
}
关于php - 如何将 'OR' 中间件用于路由 laravel 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39344836/
我是一名优秀的程序员,十分优秀!