如果在Laravel中通过身份验证,则将用户从登录页面重定向到主页 [英] Redirecting User from Login Page to Home Page if Authenticated in Laravel
问题描述
我正在使用Laravel 5.1构建登录/注册模块
I am building a login/register module using Laravel 5.1
我定义了以下路线
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.
当前,我登录并进入主页,然后输入 http://localhost:8000/即使我仍处于登录状态,也要返回登录页面.如何防止这种情况?
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?
推荐答案
检查用户是否通过身份验证,如果用户通过身份验证,则重定向到首页
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');
});
这篇关于如果在Laravel中通过身份验证,则将用户从登录页面重定向到主页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!