【问题标题】:Why Am I Able to Use user() method in laravel, without even defining it为什么我能够在 laravel 中使用 user() 方法,甚至没有定义它
【发布时间】:2015-08-18 15:22:39
【问题描述】:

我的 Laravel 5 源代码中有一个 UserControllerUser 模型。 还有一个 AuthController 也存在(使用 laravel 源预构建)。

我想使用 Eloquent 模型从刀片中的 db 查询数据。

但是,在我的 User 模型(Eloquent)和任何控制器中,都没有定义 user() 方法。即使这样,我也可以通过从Auth 类访问它来在我的刀片中使用它。为什么?

例如,

在我的刀片中,{{ Auth::user()->fname }} 有效。它从我的users 表中检索数据fname 并回显它。

它背后的逻辑是什么,我可以为其他数据库表(例如tasks)模拟相同的逻辑吗?

【问题讨论】:

    标签: php laravel eloquent laravel-5 laravel-middleware


    【解决方案1】:

    每当您自动或手动执行类似的操作时

     if (Auth::attempt(['email' => $email, 'password' => $password])) 
    {
    }
    

    所选用户的数据将存储在storage/framework/sessions

    它会有类似的数据

    a:4:{s:6:"_token";s:40:"PEKGoLhoXMl1rUDNNq2besE1iSTtSKylFFIhuoZu";s:9:"_previous";a:1:{s:3:"url";s:43:"http://localhost/Learnings/laravel5/laravel";}s:9:"_sf2_meta";a:3:{s:1:"u";i:1432617607;s:1:"c";i:1432617607;s:1:"l";s:1:"0";}s:5:"flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}
    

    上面的sessions文件没有任何数据,它会有json格式的用户id、url、token等数据。

    然后,每当您调用 {{ Auth::user()->fname }} 时,Laravel 会识别出您正在尝试获取已登录用户的 fname,然后 laravel 将获取文件并获取用户的主键并从数据库中的用户表中引用它。您可以为您拥有的所有用户table 执行此操作。

    您可以了解更多信息here

    【讨论】:

    • 这太棒了。但是,如果我想查询其他表,我将不得不为同一个表使用自定义 Eloquent 模型,而 Auth 类在这种情况下无济于事,对吗?
    • 是的。你的问题让我在我自己的网站上写了一篇文章。很快我会把Auth的优点写在我的个人网站上:)
    • 但是如果你想用内部模型来实现,你可以通过从控制器调用它将参数传递给模型的函数来实现它.. +1让我思考它:)跨度>
    【解决方案2】:

    这个用户函数在

    下定义
    vendor/laravel/framework/src/Illuminate/Auth/Guard.php
    

    内容如下:

    /**
     * Get the currently authenticated user.
     *
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function user()
    {
        if ($this->loggedOut) return;
    
        // If we have already retrieved the user for the current request we can just
        // return it back immediately. We do not want to pull the user data every
        // request into the method because that would tremendously slow an app.
        if ( ! is_null($this->user))
        {
            return $this->user;
        }
    
        $id = $this->session->get($this->getName());
    
        // First we will try to load the user using the identifier in the session if
        // one exists. Otherwise we will check for a "remember me" cookie in this
        // request, and if one exists, attempt to retrieve the user using that.
        $user = null;
    
        if ( ! is_null($id))
        {
            $user = $this->provider->retrieveById($id);
        }
    
        // If the user is null, but we decrypt a "recaller" cookie we can attempt to
        // pull the user data on that cookie which serves as a remember cookie on
        // the application. Once we have a user we can return it to the caller.
        $recaller = $this->getRecaller();
    
        if (is_null($user) && ! is_null($recaller))
        {
            $user = $this->getUserByRecaller($recaller);
    
            if ($user)
            {
                $this->updateSession($user->getAuthIdentifier());
    
                $this->fireLoginEvent($user, true);
            }
        }
    
        return $this->user = $user;
    }
    

    这个 Guard.php 中定义了更多函数,我们不时使用这些函数,甚至不知道它们来自哪里

    【讨论】:

    • 这是我的真名。我什至有我的名字的网站。 :P
    • 刚刚检查得很好。我还需要为不同的用户类型提供不同的视图,并且想知道在我的 HomeController 中定义 $this->middleware('auth'); 时是否可以实现任何类型的过滤器?喜欢使用来自users 表的我的user()->type==admin 条目?还是我应该为此创建一个单独的问题线程?
    • 你不应该发布关于所有事情的问题。首先做一些研发,如果你没有得到答案,那么只发布一个问题。我的联系方式可以在我的网站上找到,如果您有任何问题,我很乐意为您提供帮助
    【解决方案3】:

    之所以有效,是因为 Laravel 提供了不错的身份验证。

    Auth 是身份验证库,有很多类似的功能,请查看documentation

    【讨论】:

      猜你喜欢
      • 2017-05-07
      • 2022-01-05
      • 2020-10-20
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      相关资源
      最近更新 更多