php – 根据用户角色的不同控制器 – Laravel 5.1
发布时间:2020-12-14 19:37:01 所属栏目:大数据 来源:网络整理
导读:我不确定这是好事还是坏事,但我正在尝试使用不同的控制器/方法加载相同的路由,具体取决于用户角色. 试图像下面那样做一些角色过滤,但不确定这是否可行: Route::group(['before' = 'role:admin'],function() { Route::get('/','FirstController@index');});R
我不确定这是好事还是坏事,但我正在尝试使用不同的控制器/方法加载相同的路由,具体取决于用户角色.
试图像下面那样做一些角色过滤,但不确定这是否可行: Route::group(['before' => 'role:admin'],function() { Route::get('/','FirstController@index'); }); Route::group(['before' => 'role:editor'],'SecondController@index'); }); Route::filter('role',function($route,$request,$value) { // what to do here and is this the right way? }); 但是我没有让它发挥作用.我怎么能做到这一点? 编辑 找到了这个主题:Laravel same route,different controller 但接受的答案是: if( ! Auth::check()) 始终在routes.php中返回false 解决方法
也许你可以沿着这些方向找到一些东西?
Route::group(['middlware' => ['web','auth']],function (Router $router) { /** get the logged in user here **/ Auth::loginUsingId(2); $user = Auth::user(); //you can move this to some other function but just to get the idea out i did it this way. if ($user->hasRole('admin')) { $router->get('test',function () { dd('admin'); }); } elseif ($user->hasRole('owner')) { $router->get('test',function () { dd('owner'); }); } }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |