Laravel 4 Route Group

时间:2017-06-07 07:47:31

标签: laravel-4

我有两个不同的用户,我正在尝试对路由进行分组,以便其他用户无法重定向到其他用户页面。我以访客身份登录的示例,虽然只有管理员可以查看报告页面,但我可以重定向到报告页面。 我做了以下操作,但现在,我无法访问分组路线,只能访问分组以外的分组路线。 以下是我的代码:

=>过滤器

Route::filter('admin', function(){
$user = Auth::user();
if($user->type == "admin"){
     return true;
}
if (Request::ajax()){
    return Response::make('Unauthorized', 404);
}
return View::make('404_auth');
});

Route::filter('guest', function(){
$user = Auth::user();
if($user->type == "guest"){
    return true;
}
if (Request::ajax()){
    return Response::make('Unauthorized', 404);
}
return View::make('404_auth');
});

=>路线

Route::get('/', function() {
return View::make('home');
});
Route::get('home', function() {
    return View::make('home');
});
Route::get('login', function() {
    return View::make('login');
});
Route::get('register', function() {
    return View::make('register');
});

Route::post('register', 'HomeController@register');
Route::post('login', 'HomeController@login');
Route::get('logout', 'HomeController@logout');
Route::post('/', 'HomeController@postChangeLanguage');

Route::group(['before' => 'admin'], function() {
Route::get('reports', function() {
    $data = DB::table('application')->get();
    $exam_id = Apply::where('user_id', Auth::user()->id)->pluck('exam_id');
    $exam = Exam::where('id', $exam_id)->get();

    return View::make('admin/admin_side', compact('data', 'exam'));
});
Route::post('reports', 'AdminController@search');
});

Route::group(['before' => 'guest'], function() {
    Route::get('apply', function() {
        $city = Session::get('city');
        return View::make('apply', compact('city', $city));
    });
    Route::get('location', function() {
        return View::make('location');
    });
    Route::get('completed', function() {
        return View::make('getpdf');
    });
    Route::post('location', 'HomeController@location');
    Route::post('apply', 'HomeController@apply');
    Route::post('completed', 'PdfController@getpdf');
});

1 个答案:

答案 0 :(得分:0)

我认为在过滤器中,您只需返回您不想继续的情况

=>过滤器

Route::filter('admin', function(){
    $user = Auth::user();
    if($user->type != "admin"){
        return View::make('404_auth');
    }
    if (Request::ajax()){
        return Response::make('Unauthorized', 404);
    }
});

Route::filter('guest', function(){
    $user = Auth::user();
    if($user->type != "guest"){
        return View::make('404_auth');
    }
    if (Request::ajax()){
        return Response::make('Unauthorized', 404);
    } 
});

laravel 4.2 route doc

如果仍然无效,请提供更多信息,以便每个人都可以提供更多帮助。