我正在为Laravel尝试Sentry,我遇到了这个问题。我有两组路线:
Route::group(array( 'before' => 'Sentry|inGroup:Administrator'), function(){
Route::get('/', 'HomeController@index');
Route::resource('user', 'UserController');
});
Route::group(array( 'before' => 'Sentry|inGroup:Doctor'), function(){
Route::get('/', 'HomeController@index');
//Route::resource('user', 'UserController');
});
我的过滤器是:
Route::filter('inGroup', function($route, $request, $value)
{
try{
$user = Sentry::getUser();
$group = Sentry::findGroupByName($value);
//dd($user->getGroups());
//dd($user->inGroup($group));
if( ! $user->inGroup($group)){
return Redirect::to('/login')->withErrors(array(Lang::get('user.noaccess')));
}
}catch (Cartalyst\Sentry\Users\UserNotFoundException $e){
return Redirect::to('/login')->withErrors(array(Lang::get('user.notfound')));
}catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e){
return Redirect::to('/login')->withErrors(array(Lang::get('group.notfound')));
}
});
Route::filter('hasAccess', function($route, $request, $value)
{
try{
$user = Sentry::getUser();
//dd($user); $user->hasAccess($value)
if( Sentry::check() ) {
//return Redirect::to('/')->withErrors(array(Lang::get('user.noaccess')));
return Redirect::to('/');
}
}catch (Cartalyst\Sentry\Users\UserNotFoundException $e){
return Redirect::to('/login')->withErrors(array(Lang::get('user.notfound')));
}
});
问题是'Sentry|inGroup:Doctor'
的后一条路线正在开火。过滤器未获得管理员部分或组。
如何根据路由传递的参数来过滤它们?或者,我怎样才能使它们变得动态?
答案 0 :(得分:0)
您已经在第一个(管理员' s)组中定义了两次相同的 " /" 路由,并且一次进入第二个(博士)小组
Route::get('/', 'HomeController@index');
由于您未在任一路由组上指定任何前缀,因此后一(第二)路由将覆盖第一个路由,并且无法访问管理员路由。 您可以尝试使用前缀:
// http://localhost/admin
Route::group(array('prefix' => 'admin', 'before' => 'Sentry|inGroup:Admins'), function()
{
Route::get('/', 'HomeController@index');
});
// http://localhost/doctors
Route::group(array('prefix' => 'doctors', 'before' => 'Sentry|inGroup:Doctor'), function()
{
Route::get('/', 'HomeController@index');
});