何时在Laravel 5 auth trait中设置重定向路径属性
在Laravel的默认何时在Laravel 5 auth trait中设置重定向路径属性,laravel,laravel-5,Laravel,Laravel 5,在Laravel的默认AuthController类使用的AuthenticatesAndRegistersUsers特征中,使用以下代码: return redirect()->intended($this->redirectPath()); redirectPath()函数如下所示: public function redirectPath() { if (property_exists($this, 'redirectPath')) { ret
AuthController
类使用的AuthenticatesAndRegistersUsers
特征中,使用以下代码:
return redirect()->intended($this->redirectPath());
redirectPath()
函数如下所示:
public function redirectPath()
{
if (property_exists($this, 'redirectPath'))
{
return $this->redirectPath;
}
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}
阅读此代码时,我可以在AuthController
类上设置两个不同的属性:redirectPath
和redirectTo
<代码>重定向路径优先于重定向到
当我想将默认页面从
/home
重定向到/
时,我认为最好设置重定向到属性。redirectPath
属性的预期用途是什么?我认为,既然authenticates和registersusers
是一种特性,可以由不同的类使用,两个属性检查都是为了与不同的类向后兼容,但逻辑具有相同的目的
通常我建议您使用重定向路径
属性,因为这是该函数中的第一个条件,如果出于任何原因,您没有注意并使用控制器中的重定向到属性,该属性扩展了另一个定义了重定向路径的控制器,那么您最终将思考出了什么问题,以及为什么控制器将您重定向到不同的路径。我找到了一些问题这些属性和重定向路径()函数的历史记录
2014年11月30日
重定向最初是在AuthenticatesAndRegistersUsers
trait中硬编码的。
+返回重定向('/home');
重定向已更改为redirect($this->redirectTo)
-返回重定向('/home');
+返回重定向($this->redirectTo);
2014年12月1日
添加了重定向路径()
函数
+公共函数重定向路径()
+ {
+返回属性_存在($this,'redirectTo')?$this->redirectTo:'/home';
+ }
并将重定向更改为redirect($this->redirectPath())
-返回重定向($this->redirectTo);
+返回重定向($this->redirectPath());
同时,删除了AuthController
中的属性
-protected$redirectTo='/home';
2015年2月7日
redirectPath
属性已添加到redirectPath()
函数:
公共函数重定向路径()
{
+如果(属性_存在($this,'redirectPath'))
+ {
+返回$this->redirectPath;
+ }
返回属性_存在($this,'redirectTo')?$this->redirectTo:'/home';
}
结论
看起来要使用的正确属性是redirectPath
,因为它与redirectPath()
函数一致。它还设计用于覆盖可能已添加的任何较旧的重定向到
属性。它看起来不像AuthController
类在默认情况下设置了重定向路径
属性(他们曾设置过重定向到
属性)。其他一切似乎都是对的,所以我把它标为公认的答案。请参阅我的答案,了解我挖掘的更多细节。啊,对不起,也许你是对的。我在我的一个项目上查看了AuthController
,但似乎默认情况下该属性不在那里,我已经手动添加了它。很抱歉,我将编辑我的答案。很好…*掌声*