Laravel的redirectTo()方法发生了什么? [英] What happened to Laravel's redirectTo() method?

查看:118
本文介绍了Laravel的redirectTo()方法发生了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以覆盖此属性以在LoginController中登录后重定向用户:

We can override this property to redirect users after login in LoginController:

protected $redirectTo = '/home';

这是文档中的声明:

如果重定向路径需要自定义生成逻辑,则可以定义一个 redirectTo方法而不是redirectTo属性:

If the redirect path needs custom generation logic you may define a redirectTo method instead of a redirectTo property:

受保护的函数redirectTo(){
//}

protected function redirectTo() {
// }

但是无论情况如何,它始终会重定向到'/home';.

But it always redirects to '/home'; whatever the condition is.

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.

但是,覆盖authenticated()sendLoginResponse()函数可以正常工作.

However overriding authenticated() or sendLoginResponse() function works fine.

protected function authenticated()
    {
      if (Auth::user()->role==0) {
        return redirect('/volunteer') ;
      } else {
        return redirect('/donor');
      }
    }

那里的redirectTo()方法有什么问题?这是 GitHub源代码这些方法.

What is wrong with redirectTo() method there? Here is the GitHub source code to these methods.

我正在使用 Laravel版本5.3.28 .

推荐答案

这是 Laravel v5.3.28 中src/Illuminate/Foundation/Auth/RedirectsUsers.php中的redirectPath()方法>

This is the redirectPath() method in src/Illuminate/Foundation/Auth/RedirectsUsers.php in Laravel v5.3.28

public function redirectPath()
{
    return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}

在更高版本 5.3.29及更高版本中.这已通过提交在文件中更改:

In the later versions 5.3.29 and above. This was changed in file with commit:

添加auth重定向路径生成方法(#16896)

 public function redirectPath()
 {
     if (method_exists($this, 'redirectTo')) {
         return $this->redirectTo();
     }
     return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
 }

因此,本部分文档仅适用于Laravel 5.3.29版和更高版本

如果重定向路径需要自定义生成逻辑,则可以定义一个 redirectTo方法而不是redirectTo属性:

If the redirect path needs custom generation logic you may define a redirectTo method instead of a redirectTo property:

受保护的函数redirectTo(){//}

protected function redirectTo() { // }


v5.3.28解决方案

要使redirectTo()方法在v5.3.28中工作,请在src/Illuminate/Foundation/Auth/RedirectsUsers.php中的redirectPath()方法中手动添加此方法.

To make redirectTo() method work in v5.3.28, manually add this in redirectPath() method in src/Illuminate/Foundation/Auth/RedirectsUsers.php.

if (method_exists($this, 'redirectTo')) {
    return $this->redirectTo();
}

这篇关于Laravel的redirectTo()方法发生了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆