我在 Laravel 5.2 应用程序中的路由方面遇到了困难。
预期的行为是:
- 未注册用户尝试访问 protected 页面
- 重定向至登录
- 重定向(成功时)到预期页面
当然,如果没有预期的页面(当他主动登录时),用户应该被重定向到他的“仪表板”
示例:
/* Create a Payment | Only for registered Users (client) */
Route::post('payment', 'PaymentController@create')->middleware('client');
用户将被重定向到登录页面,但是,登录后,他将被重定向到我在 authcontroller 中定义为 $redirectTo 的页面
protected $redirectTo = '/backoffice';
有没有办法将重定向设置为类似(预期或默认值)
最佳答案
我发现AuthenticatesUsers.php
中定义的AuthenticatesUsers Trait
具有函数handleUserWasAuthenticated
,它检查Laravel是否默认身份验证AuthController。 php
是否定义了authenticated
方法。如果未定义此方法,则用户始终会重定向到您在 $redirectTo
属性中定义的任何内容。它不会将用户重定向到预期的 URL。但是,在 AuthController.php 中,如果您按如下方式定义方法:
protected function authenticated (Request $request, $user){
//add ur custom actions after the user is authenticated
}
redirect()->intended()
将完全按照您的预期工作,并且在用户通过身份验证后,他将被重定向到预期的 URL。
关于php - laravel 5.2 登录后重定向到预期的网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36695762/