laravel `Auth:user()` 或 `Auth:id()` 是如何工作的

How laravel `Auth:user()` or `Auth:id()` works

laravel Auth:user()Auth:id() 的工作原理

它是驻留在会话中还是数据库中。

我搜索了但没有找到好文章。

请帮忙理解。我知道我会得到很多反对票 ;)

你读了吗?这是一个很好的入门指南

https://laravel.com/docs/5.4/authentication

laravel 使用会话 authentication.if 您是 laravel 的初学者,那么必须阅读以下内容 link:

https://laravel.com/docs/5.4/authentication

我觉得对你有帮助

以下是我试图弄清楚 Auth::user() 调用中实际发生的情况:

Auth::user()

Illuminate\Support\Facades\Auth
extends Illuminate\Support\Facades\Facade

Facade::__callStatic('user')

static::getFacadeRoot()

resolveFacadeInstance(static::getFacadeAccessor == 'auth' (from Auth class))

return static::$app[$name];
static::$app is instance of Illuminate\Foundation\Application
extends Illuminate\Container\Container

实现了 ArrayAccess(这就是 $obj[] 语法有效的原因)

Container::offsetGet(auth)

Application::make(auth) 

Container::getAlias(auth) return 'auth'

Container::make(auth)

Container::resolve(auth)

亚达,亚达,亚达 参见 Application::registerCoreContainerAliases

'auth' = Illuminate\Auth\AuthManager

AuthManager::user() = AuthManager::__call = $this->guard()->user()

AuthManager::guard(web)

AuthManager::resolve(web) (see config/auth.php)

AuthManager::createSessionDriver() returns new Illuminate\Auth\SessionGuard

SessionGuard::user() // <---- this is what actually get's called, based on default config

您可以在 Auth\SessionGuard class 中找到此方法:

Authenticatable|null user()

Get the currently authenticated user.

Return Value Authenticatable|null

查看:https://laravel.com/api/5.7/Illuminate/Auth/SessionGuard.html#method_user