通过调用AuthManager可以拼接得出,最终他会调用一个createSessionDriver方法:
public function createSessionDriver($name, $config) { $provider = $this->createUserProvider($config[\'provider\']); $guard = new SessionGuard($name, $provider, $this->app[\'session.store\']); if (method_exists($guard, \'setCookieJar\')) { $guard->setCookieJar($this->app[\'cookie\']); } if (method_exists($guard, \'setDispatcher\')) { $guard->setDispatcher($this->app[\'events\']); } if (method_exists($guard, \'setRequest\')) { $guard->setRequest($this->app->refresh(\'request\', $guard, \'setRequest\')); } return $guard; }所以最终是调用了SessionGuard的login方法来完成session部分的功能。
登录展示表单的部分就不赘述了,这里我们直接看:
$this->post(\'login\', \'Auth\LoginController@login\'); //LoginController.php class LoginController extends Controller { use AuthenticatesUsers; /** * Where to redirect users after login. * * @var string */ protected $redirectTo = \'/home\'; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware(\'guest\')->except(\'logout\'); } }他使用到了一个AuthenticatesUserstrait,里面有一个login方法,也就是我们要使用到的login了:
public function login(Request $request) { $this->validateLogin($request); if ($this->hasTooManyLoginAttempts($request)) { $this->fireLockoutEvent($request); return $this->sendLockoutResponse($request); } if ($this->attemptLogin($request)) { return $this->sendLoginResponse($request); } $this->incrementLoginAttempts($request); return $this->sendFailedLoginResponse($request); }其实大部分和register的部分差不多,核心部分还是在于SessionGuard对象的获取,这里就不过多赘述了,不过值得一提的是,像Auth::check()等很多方法的使用,其实也会通过SessionGuard来完成,主要是通过:
public function __call($method, $parameters) { return $this->guard()->{$method}(...$parameters); } public function check() { return ! is_null($this->user()); }来得以完成调度。