laravel - 何时在 Laravel 5 auth 特征中设置 redirectPath 属性

标签 laravel laravel-5

在 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 类上设置两个不同的属性:redirectPathredirectToredirectPath 优先于 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/

相关文章:

laravel - 如何为 Laravel 5.7 创建自定义 Auth Guard/Provider

php - Laravel 4 Fluent 计数

Laravel @push 在 create_category.blade.php 中不起作用

linux - Composer在您的平台中检测到问题: Your Composer dependencies require a PHP version ">= 7.3.0"

php - Laravel Eloquent 一对多关系不起作用

javascript - 将 JavaScript 数组从 View 传递到 Laravel Controller

php - Laravel Eloquent : Get record that contains two specific relations

php - Laravel App Utility 类的最佳位置

php - 在 Laravel 中,如何获取公共(public)文件夹中所有文件的列表?

php - Laravel 错误 : symlink() has been disabled for security reasons