/** * Attempt to get the guard from the local cache. * * @param string $name * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard */ publicfunctionguard($name = null) { $name = $name ?: $this->getDefaultDriver();
if (method_exists($this, $driverMethod)) { return$this->{$driverMethod}($name, $config); }
thrownewInvalidArgumentException("Auth driver [{$config['driver']}] for guard [{$name}] is not defined."); }
可以看到,默认情况下就会调用到 createTokenDriver 。来看看这个方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
publicfunctioncreateTokenDriver($name, $config) { // The token guard implements a basic API token based guard implementation // that takes an API token field from the request and matches it to the // user in the database or another persistence layer where users are. $guard = newTokenGuard( $this->createUserProvider($config['provider'] ?? null), $this->app['request'] );
/** * Get the currently authenticated user. * * @return \Illuminate\Contracts\Auth\Authenticatable|null */ publicfunctionuser() { // If we've already retrieved the user for the current request we can just // return it back immediately. We do not want to fetch the user data on // every call to this method because that would be tremendously slow. if (! is_null($this->user)) { return$this->user; }