2016-02-22 85 views
2

我是laravel的新手,我安裝了auth scaffold,這裏是到目前爲止的路由。laravel 5.2如何保護只有經過認證的用戶的路由

Route::group(['middleware' => ['web']], function() { 

    Route::auth(); 
    Route::get('/home', '[email protected]');  


    Route::get('addthreadhtml', function() 
    { 
     return View::make('addThreadForm'); 
    }); 
    Route::post('thread/add', '[email protected]'); 
    Route::get('thread/showall', '[email protected]'); 

}); 

我要保護addthreadhtml非認證用戶訪問,如果用戶還沒有登錄,他們將被重定向到另一個視圖。

我該怎麼做?

+0

你應該谷歌「訪問控制列表laravel」https://mattstauffer.co/blog/acl-access-control-list-authorization-in-laravel-5- 1 – Unex

+0

https://laracasts.com/discuss/channels/laravel/protecting-admin-routes-laravel-42 –

+0

使用'auth'中間件:https://laravel.com/docs/master/middleware#assigning-middleware -to-路線 –

回答

1

,如果你想爲函數的權威性而已,你可以把這個在上面的代碼

if(!Auth::user('id')){ 
    //redirect to any view not require auth 
} 

或本代碼

if (Auth::check()) { 
    // The user is logged in... 
} 
2

的權威性中間件添加到路由要保護:

Route::get('addthreadhtml', ['middleware' => 'auth', function() { 
    return View::make('addThreadForm'); 
}]);