Laravel:哨兵路线和过滤器组

Laravel: Sentry Routes and Filter groups

我正在为 Laravel 尝试 Sentry,我遇到了这个问题。我有 2 组路线:

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' 的后一条路线正在触发。过滤器未获取管理员部分或组。

如何根据路由传递的参数来过滤它们?或者,我怎样才能使它们动态化?

您定义了两次相同的 "/" 路由,一次在第一个(管理员)组,一次在第二个(博士)组

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');
});