在AuthenticatesAndRegistersUsersLaravel的默认AuthController类使用的特性中,使用以下代码:
return redirect()->intended($this->redirectPath());
Run Code Online (Sandbox Code Playgroud)
该redirectPath()功能如下:
public function redirectPath()
{
if (property_exists($this, 'redirectPath'))
{
return $this->redirectPath;
}
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}
Run Code Online (Sandbox Code Playgroud)
阅读此代码,我可以在AuthController类上设置两个不同的属性:redirectPath和redirectTo.redirectPath优先于redirectTo.
当我想更改默认页面以重定向/home到/我时,我认为最好设置redirectTo属性.该redirectPath物业的用途是什么?
我在这些属性和redirectPath()功能上挖掘了一些历史.
重定向在AuthenticatesAndRegistersUsers特征中被粗略地编码.
https://github.com/laravel/framework/commit/cc1c35069a7bbc3717487d931fbd80b8e6641a90
+ return redirect('/home');
Run Code Online (Sandbox Code Playgroud)
重定向更改为redirect($this->redirectTo)
https://github.com/laravel/framework/commit/a71926653a573f32ca7a31527c7644c4305c1964#diff-b72935cc9bfd1d3e8139fd163ae00bf5
- return redirect('/home');
+ return redirect($this->redirectTo);
Run Code Online (Sandbox Code Playgroud)
redirectPath()功能已添加
https://github.com/laravel/framework/commit/dd78c4fe763859d11e726477125b7d1a00c860c0#diff-b72935cc9bfd1d3e8139fd163ae00bf5
+ public function redirectPath()
+ {
+ return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
+ }
Run Code Online (Sandbox Code Playgroud)
并且重定向更改为 redirect($this->redirectPath())
- return redirect($this->redirectTo);
+ return redirect($this->redirectPath());
Run Code Online (Sandbox Code Playgroud)
同时,AuthController删除
了属性https://github.com/laravel/laravel/commit/57a6e1ce7260444719dd3de1fdd7c58cdcdba362
- protected $redirectTo = '/home';
Run Code Online (Sandbox Code Playgroud)
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';
}
Run Code Online (Sandbox Code Playgroud)
看起来正确使用的属性redirectPath是与redirectPath()函数一致的.它还旨在覆盖redirectTo可能已添加的任何旧属性.
| 归档时间: |
|
| 查看次数: |
972 次 |
| 最近记录: |