在 Laravel 的默认 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
。 redirectPath
优先于 redirectTo
。
当我想要更改默认页面以从 /home
重定向到 /
时,我认为最好设置 redirectTo
属性(property)。 redirectPath
属性的预期用途是什么?
最佳答案
我挖掘了这些属性和 redirectPath()
的一些历史记录。功能。
2014 年 11 月 30 日
重定向最初硬编码在 AuthenticatesAndRegistersUsers
中特征。
https://github.com/laravel/framework/commit/cc1c35069a7bbc3717487d931fbd80b8e6641a90
+ return redirect('/home');
重定向已更改为 redirect($this->redirectTo)
https://github.com/laravel/framework/commit/a71926653a573f32ca7a31527c7644c4305c1964#diff-b72935cc9bfd1d3e8139fd163ae00bf5
- return redirect('/home');
+ return redirect($this->redirectTo);
2014 年 12 月 1 日
redirectPath()
添加了功能
https://github.com/laravel/framework/commit/dd78c4fe763859d11e726477125b7d1a00c860c0#diff-b72935cc9bfd1d3e8139fd163ae00bf5
+ public function redirectPath()
+ {
+ return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
+ }
重定向更改为 redirect($this->redirectPath())
- return redirect($this->redirectTo);
+ return redirect($this->redirectPath());
同时,AuthController
中的属性被删除
https://github.com/laravel/laravel/commit/57a6e1ce7260444719dd3de1fdd7c58cdcdba362
- protected $redirectTo = '/home';
2015 年 2 月 7 日
redirectPath
属性已添加到 redirectPath()
功能:
https://github.com/laravel/framework/commit/63a534a31129be4cec4f5a694342d7020e2d7f07#diff-b72935cc9bfd1d3e8139fd163ae00bf5
public function redirectPath()
{
+ if (property_exists($this, 'redirectPath'))
+ {
+ return $this->redirectPath;
+ }
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}
结论
看起来要使用的正确属性是 redirectPath
因为它与 redirectPath()
一致功能。它还旨在覆盖任何旧的 redirectTo
可能已添加的属性。
关于laravel - 何时在 Laravel 5 auth 特征中设置 redirectPath 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30425208/