[英]Laravel 5.2 Auth routing
I am having an issue with laravel routing. 我在laravel路由方面遇到问题。
I want to have routes like this: 我想要这样的路线:
/ - home page for unauthenticated users
/login - login page
/register - register page
/dashboard - home page for authenticated users
After login i want user to be redirected to /dashboard, and if authenticated user goes to / or any other unprotected route, i also want to redirect him to /dashboard. 登录后,我希望用户重定向到/ dashboard,并且如果通过身份验证的用户转到/或任何其他不受保护的路由,我也希望将他重定向到/ dashboard。
My routes.php. 我的routes.php。
`Route::get('/', 'HomeController@index');
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get(‘/dashboard’, ‘DashboardController@index');
Route::get('/logout', 'Auth\AuthController@logout');
});`
This works, however if authenticated user goes to / or any other unprotected route, i would like to redirect him to /dashboard. 这可行,但是如果经过身份验证的用户转到/或任何其他不受保护的路由,我想将他重定向到/ dashboard。 How can i make this work?
我该如何工作?
Taken from Laravel docs. 取自Laravel文档。
Path Customization 路径定制
When a user is successfully authenticated, they will be redirected to the / URI. 当用户成功通过身份验证后,他们将被重定向到/ URI。 You can customize the post-authentication redirect location by defining a redirectTo property on the AuthController:
您可以通过在AuthController上定义redirectTo属性来自定义身份验证后的重定向位置:
protected $redirectTo = '/home';
When a user is not successfully authenticated, they will be redirected back to the login form location automatically. 当用户未成功通过身份验证时,他们将被自动重定向回登录表单位置。
See more here. 在这里查看更多。 https://laravel.com/docs/5.2/authentication#included-routing
https://laravel.com/docs/5.2/authentication#included-routing
you need to set :: 您需要设置::
protected $redirectTo = '/home' 保护$ redirectTo ='/ home'
in the AuthController which will override the $redirectTo variable in the Trait used by AuthController. 在AuthController中,它将覆盖AuthController使用的Trait中的$ redirectTo变量。
u can also change redirectAfterLogout url in the same way. 您还可以通过相同的方式更改redirectAfterLogout网址。
!!Happy Coding. !!快乐编码。
In your HomeController@index
method, do a check and redirect the Auth user to dashboard. 在您的
HomeController@index
方法中,进行检查并将Auth用户重定向到仪表板。 Auth::check() ? return redirect()->url('/dashboard') : '';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.