[英]Redirecting User from Login Page to Home Page if Authenticated in Laravel
I am building a login/register module using Laravel 5.1 我正在使用Laravel 5.1构建登录/注册模块
I have defined the following routes 我定义了以下路线
Route::get('/', function () {
return view('welcome');
});
Route::get('/home', ['middleware' => 'auth', function () {
return view('home');
}]);
I have also created a view for welcome and home. 我还创建了一个欢迎回家的视图。 When I enter my credentials and login, I get redirected to the home page. 输入凭据并登录后,我将重定向到主页。
How do I make sure that once I am authenticated and I try to access the '/' route, I get redirected to the home page. 如何确保通过身份验证并尝试访问“ /”路由后,将重定向到主页。
Currently once I am logged in and reach the home page and I type http://localhost:8000/ I get back to the login page even though I am still logged in. How can I prevent this? 当前,一旦我登录并进入主页并键入http:// localhost:8000 /,即使我仍在登录,我也返回登录页面。如何防止这种情况发生?
Check If user is authenticated and if user is authenticated then redirect to home 检查用户是否通过身份验证以及用户是否通过身份验证,然后重定向到首页
Route::get('/', function () {
if(Auth::check()){return Redirect::to('home');}
return view('welcome');
});
I would approach this the other way around. 我将以另一种方式处理此问题。 I would always go to home, and if they are not authenticated, redirect them to the login page. 我将始终回家,如果未通过身份验证,请将其重定向到登录页面。
In your controller: 在您的控制器中:
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest:user', ['except' => ['logout']]);
}
(with user is the auth logging in) (用户是登录的身份验证)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.