Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么我可以在laravel中使用user()方法,而不用定义它_Php_Laravel_Eloquent_Laravel 5_Laravel Middleware - Fatal编程技术网

Php 为什么我可以在laravel中使用user()方法,而不用定义它

Php 为什么我可以在laravel中使用user()方法,而不用定义它,php,laravel,eloquent,laravel-5,laravel-middleware,Php,Laravel,Eloquent,Laravel 5,Laravel Middleware,我的Laravel 5源代码中确实有一个UserController和User模型。 另外还有一个AuthController(随laravel源代码预构建) 我想使用雄辩的模型从我的刀片服务器中的db查询数据 但是,无论是在myUser模型(Eloquent)中还是在任何控制器中,都没有定义User()方法。即使这样,我也可以通过从Auth类访问它来在我的刀片中使用它。为什么? 比如说, 在我的刀片中,{{Auth::user()->fname}可以工作。它从myusers表中检索数据fnam

我的Laravel 5源代码中确实有一个
UserController
User
模型。 另外还有一个
AuthController
(随laravel源代码预构建)

我想使用雄辩的模型从我的刀片服务器中的db查询数据

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

比如说,

在我的刀片中,
{{Auth::user()->fname}
可以工作。它从my
users
表中检索数据
fname
,并进行回显


它背后的逻辑是什么?我可以对其他db表(如
任务
)进行同样的模拟吗?

它可以工作,因为Laravel具有良好的身份验证


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

每当您自动或手动执行此操作时

 if (Auth::attempt(['email' => $email, 'password' => $password])) 
{
}
所选用户的数据将存储在
存储/框架/会话中

它会有类似的数据

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:{}}}
上面的会话文件没有任何数据,它将包含json格式的数据,例如用户id、url、令牌

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


您可以了解有关它的更多信息

此用户函数在下定义

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中定义了更多的函数,我们时不时地使用这些函数,甚至不知道它们来自哪里,这真是太棒了。但是,如果我想查询其他表,我必须为同一个表使用自定义的雄辩模型,而Auth类在这种情况下不会有帮助,对吗?是的。。你的问题让我不得不在自己的网站上写一篇文章。。很快,我将在我的个人网站上写一篇文章来说明
Auth
的优点:)但是如果你想通过内部模型实现,你可以通过从控制器调用param将其传递给模型的函数来实现+谢谢你让我思考:)那是我的真名。我甚至有我名字的网站我刚检查好。我还需要为不同的用户类型提供不同的视图,并想知道在定义
$this->middleware('auth')时是否可以实现任何类型的过滤器在我的家庭控制器中?像是使用我的
user()->type==admin
来自
users
表的条目吗?或者我应该为此创建一个单独的问题线索吗?你不应该发布所有问题。首先做一些研发,如果你没有得到答案,那么只发布一个问题。我的联系方式可以在我的网站上找到,如果你有任何问题,我很乐意帮助你