Laravel 的 redirectTo() 方法发生了什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42024477/
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
What happened to Laravel's redirectTo() method?
提问by Sanzeeb Aryal
We can override this property to redirect users after login in LoginController:
我们可以覆盖此属性以在登录 LoginController 后重定向用户:
protected $redirectTo = '/home';
And here is the statement from documentation:
这是文档中的声明:
If the redirect path needs custom generation logic you may define a redirectTo method instead of a redirectTo property:
protected function redirectTo() {
// }
如果重定向路径需要自定义生成逻辑,您可以定义 redirectTo 方法而不是 redirectTo 属性:
受保护的函数redirectTo() {
// }
But it always redirects to '/home';
whatever the condition is.
但它总是重定向到'/home';
任何条件。
protected function redirectTo()
{
if (Auth::user()->role==0) {
return '/volunteer';
} else {
return '/donor';
}
}
If a method exists it'll use it, otherwise the property will be used.But it looks like property is being used even if the method exists.
如果方法存在,它将使用它,否则将使用该属性。但即使该方法存在,它看起来也正在使用属性。
However overriding authenticated()
or sendLoginResponse()
function works fine.
然而,覆盖authenticated()
或sendLoginResponse()
功能工作正常。
protected function authenticated()
{
if (Auth::user()->role==0) {
return redirect('/volunteer') ;
} else {
return redirect('/donor');
}
}
What is wrong with redirectTo()
method there? Here is the GitHub source codeto these methods.
redirectTo()
那里的方法有什么问题?这是这些方法的GitHub 源代码。
I'm using Laravel version 5.3.28.
我正在使用Laravel 版本 5.3.28。
采纳答案by Sanzeeb Aryal
This is the redirectPath()
method in src/Illuminate/Foundation/Auth/RedirectsUsers.php in Laravel v5.3.28
这是Laravel v5.3.28redirectPath()
中src/Illuminate/Foundation/Auth/RedirectsUsers.php中的方法
public function redirectPath()
{
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}
In the later versions 5.3.29 and above. This was changed in file with commit:
在5.3.29 及以上版本中。这在提交时在文件中更改:
Add auth redirect path generation method (#16896)
public function redirectPath()
{
if (method_exists($this, 'redirectTo')) {
return $this->redirectTo();
}
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}
So this part of documentation applies for Laravel version 5.3.29 and later only
所以这部分文档仅适用于 Laravel 5.3.29 及更高版本
If the redirect path needs custom generation logic you may define a redirectTo method instead of a redirectTo property:
protected function redirectTo() { // }
如果重定向路径需要自定义生成逻辑,您可以定义 redirectTo 方法而不是 redirectTo 属性:
受保护的函数redirectTo() { // }
Solution for v5.3.28
v5.3.28 解决方案
To make redirectTo()
method work in v5.3.28, manually add this in redirectPath()
method in src/Illuminate/Foundation/Auth/RedirectsUsers.php.
要使redirectTo()
方法在 v5.3.28 中工作,请redirectPath()
在 src/Illuminate/Foundation/Auth/RedirectsUsers.php中的方法中手动添加此方法。
if (method_exists($this, 'redirectTo')) {
return $this->redirectTo();
}
回答by Troyer
Simple solution
简单的解决方案
Override redirectPath()
instead of redirectTo()
.
覆盖redirectPath()
而不是redirectTo()
.
Using raw string return:
使用原始字符串返回:
protected function redirectPath()
{
if (Auth::user()->role==0) {
return '/volunteer';
} else {
return '/donor';
}
}
Or overriding redirectPath()
to the Laravel 5.3.29redirectPath()
version and then your redirectTo()
method will work.
或者覆盖redirectPath()
到 Laravel 5.3。29redirectPath()
版本,然后您的redirectTo()
方法将起作用。
public function redirectPath()
{
if (method_exists($this, 'redirectTo')) {
return $this->redirectTo();
}
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}
Why redirectTo() is not working
为什么 redirectTo() 不起作用
Tested overriding the redirectPath()
or redirectTo()
method in App\Http\Controllers\Auth\LoginController.php
on a clean Laravel v.5.3.29 + default Auth, they work as expected.
在干净的 Laravel v.5.3.29 + 默认身份验证中测试覆盖redirectPath()
orredirectTo()
方法App\Http\Controllers\Auth\LoginController.php
,它们按预期工作。
Example of redirectTo() method
redirectTo() 方法示例
Documentation says:
文档说:
If the redirect path needs custom generation logic you may define a redirectTo method instead of a redirectTo property.
如果重定向路径需要自定义生成逻辑,您可以定义 redirectTo 方法而不是 redirectTo 属性。
So, the function should look something like this:
因此,该函数应如下所示:
protected function redirectTo()
{
if(condition) {
return "/your/path";
}
return "/your/secondpath";
}
回答by Arthur Tarasov
redirectTo
redirects users that complete login or registration. Users who are already logged in and try to register or log in again will be redirected by app/Http/Middleware/RedirectIfAuthenticated.php
redirectTo
重定向完成登录或注册的用户。已经登录的用户尝试注册或再次登录将被重定向app/Http/Middleware/RedirectIfAuthenticated.php
if (Auth::guard($guard)->check()) {
return redirect('/home');
}
回答by Thungdemo
I think the issue comes from the web.php routes. I problem went away after I set the default '/' route to point to the login page.
我认为问题来自 web.php 路由。在我将默认的“/”路由设置为指向登录页面后,我的问题就消失了。
Route::get('/', function () {
return redirect(route('login'));
});