如果用户未登录Laravel,则重定向到登录页面

Pra*_*san 1 laravel laravel-routing laravel-5

我正在使用Laravel版本5.我有一个包含大量行的项目的路径文件.如果用户没有登录,我需要将像Redirect这样的身份验证放到登录页面.如果用户没有登录,我还想阻止直接访问URL.我该怎么办?

我使用下面的代码

Route::group(array('before' => 'auth'), function(){
    Route::get('/', 'HomeController@index');
});
Run Code Online (Sandbox Code Playgroud)

但这只能阻止主页.我需要保护我的所有路线.并建议我如何减少路由文件行.

chr*_*con 5

您可以根据需要在组中放置尽可能多的路径

Route::group(array('before' => 'auth'), function(){
    Route::get('/', 'HomeController@index');
    Route::post('store', 'HomeController@store');
    Route::get('show', 'AnotherController@index');
    // ...
});
Run Code Online (Sandbox Code Playgroud)

如果您真的需要保护所有路线,可以添加

 public function __construct()
 {
      $this->middleware('auth');
 }
Run Code Online (Sandbox Code Playgroud)

到你的主控制器类, Http/Controllers/Controller

根据您可能使用的文档编辑您的评论

return redirect() - > intention('view');

/**
 * Handle an authentication attempt.
 *
 * @return Response
 */
    public function authenticate()
    {
        if (Auth::attempt(['email' => $email, 'password' => $password]))
        {
            return redirect()->intended('dashboard');
        }
    }

}
Run Code Online (Sandbox Code Playgroud)