登录后,laravel 5.2 重定向到预期的 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36695762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel 5.2 redirect to intended url after login
提问by Frank Provost
I'm struggeling with my routing in my laravel 5.2 application.
我正在为我的 Laravel 5.2 应用程序中的路由而苦苦挣扎。
The intended behaviour is:
预期的行为是:
- Unregistered User trys to access protected page
- Redirected to Login
- Redirected (on success) to intended page
- 未注册用户尝试访问受保护页面
- 重定向到登录
- 重定向(成功)到预期页面
Of course the user should be redirected to his "dashboard" if there is no intended page (when he activly logged in)
当然,如果没有预期页面(当他主动登录时),用户应该被重定向到他的“仪表板”
Example:
例子:
/* Create a Payment | Only for registered Users (client) */
Route::post('payment', 'PaymentController@create')->middleware('client');
The User will be redirected to the login page, however, after the login, he will be redirected to the page i defined in my authcontroller as $redirectTo
用户将被重定向到登录页面,但是,登录后,他将被重定向到我在我的 authcontroller 中定义为 $redirectTo 的页面
protected $redirectTo = '/backoffice';
Is there a way to set the redirect to something like (inteded ordefault)
有没有办法将重定向设置为类似(inteded或default)
采纳答案by scorer
If you want a not logged-in user to redirect to a login page and after a successful log-in to return back to intended url or the default that is setup in your variable.
如果您希望未登录的用户重定向到登录页面,并在成功登录后返回到预期的 url 或变量中设置的默认值。
protected $redirectTo = '/backoffice';
Then probably you would use something like this.
那么可能你会使用这样的东西。
return redirect()->guest('login');
Laravel will keep the "intended.url" in session for you and after successful login redirect user back to /backoffice.
Laravel 将在会话中为您保留“intended.url”,并在成功登录后将用户重定向回 /backoffice。
Here is another answer about various Laravel versions
这是关于各种 Laravel 版本的另一个答案
回答by Wael Showair
I found out that AuthenticatesUsers Trait
defined in AuthenticatesUsers.php
has function handleUserWasAuthenticated
where it checks if Laravel default authentication AuthController.php
has a method authenticated
defined or not. If this method is not defined, the user is always redirect to whatever you define in $redirectTo
property. It does not redirect the user to the intended URL. However, in AuthController.php
if you define the method as follows:
我发现AuthenticatesUsers Trait
在AuthenticatesUsers.php
has 函数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()
will work as you expect exactly and after the user is authenticated, he will be redirected to the intended URL.
redirect()->intended()
将完全按照您的预期工作,并且在用户通过身份验证后,他将被重定向到预期的 URL。
回答by Mevlüt?zdemir
I'm using Laravel 5.4
我正在使用 Laravel 5.4
LoginController:
登录控制器:
public function __construct()
{
session(['url.intended' => url()->previous()]);
$this->redirectTo = session()->get('url.intended');
$this->middleware('guest')->except('logout');
}
回答by Jewel
In 5.2 Only change in "app/Http/Controllers/Auth/AuthController.php" near line no 31 protected $redirectTo = '/Your_View_page';
在 5.2 中,仅在第 31 行附近的“app/Http/Controllers/Auth/AuthController.php”中更改 protected $redirectTo = '/Your_View_page';
回答by DavidLyonsGarcia
This solution will work perfectly with Laravel 5 and 6, I'm currently working on Laravel 6.0.*
此解决方案将与 Laravel 5 和 6 完美配合,我目前正在研究 Laravel 6.0。*
Just add this function to the app\Http\Controllers\Auth\loginController.php:
只需将此函数添加到 app\Http\Controllers\Auth\loginController.php 中:
public function redirectTo()
{
$finalRedirectionTo = \Session::get('url.intended', $this->redirectTo);
return $finalRedirectionTo;
}
And that's it! Because de RedirectUsers Laravel trait that is implemented by AuthenticatesUsers trait that your LoginController should also 'use', checks if the function redirectTo() exists and call it to return the url of the redirection.
就是这样!因为 de RedirectUsers Laravel trait 由 AuthenticatesUsers trait 实现,您的 LoginController 也应该“使用”,所以检查函数 redirectTo() 是否存在并调用它以返回重定向的 url。
It works with social and local accounts
它适用于社交和本地帐户
I hope it helps! David Lyons
我希望它有帮助!大卫·莱昂斯