Laravel auth登录之怎么通过guard流程

7个月前 0 0 1804

来看看这个if判断

if (Auth::guard($this->getGuard())->attempt($credentials, $request->has('remember'))) {
            return $this->handleUserWasAuthenticated($request, $throttles);
        }

先来看一下这个方法getGuard();

protected function getGuard()
{
   return property_exists($this, 'guard') ? $this->guard : null;//又是一个属性的设置,干嘛的呢?现在似乎看不出来,继续往下,返回null值
}

Auth::guard(null);又是静态方法,很明显啦,又是门面,查一下手册门面表Auth就是IlluminateAuthAuthManager,找到guard方法!

public function guard($name = null)
    {
        $name = $name ?: $this->getDefaultDriver();//进入到方法看一下获取的是web,配置文件一共有种方式web和api,所以啦,上面“guard”属性还可以设置api

        return isset($this->guards[$name])//返回false
                    ? $this->guards[$name]
                    : $this->guards[$name] = $this->resolve($name);//进入到resolve方法看看
    }
protected function resolve($name)
    {
        $config = $this->getConfig($name);//返回这样的数组['driver' => 'session','provider' => 'users',],
        
        if (is_null($config)) {
            throw new InvalidArgumentException("Auth guard [{$name}] is not defined.");
        }
        //$this->customCreators此时是空数组
        if (isset($this->customCreators[$config['driver']])) {
            return $this->callCustomCreator($name, $config);
        } else {
            $driverMethod = 'create'.ucfirst($config['driver']).'Driver';//createSessionDriver方法
            if (method_exists($this, $driverMethod)) {
                return $this->{$driverMethod}($name, $config);
            } else {
                throw new InvalidArgumentException("Auth guard driver [{$name}] is not defined.");
            }
        }
    }

好了,找到createSessionDriver方法!

    public function createSessionDriver($name, $config)

    {

        $provider = $this->createUserProvider($config['provider']);//返回Eloquent服务驱动或是db服务驱动,根据auth配置文件配置

        $guard = new SessionGuard($name, $provider, $this->app['session.store']);

        if (method_exists($guard, 'setCookieJar')) {//设置cookie

            $guard->setCookieJar($this->app['cookie']);

        }

        if (method_exists($guard, 'setDispatcher')) {//设置调度

            $guard->setDispatcher($this->app['events']);

        }

        if (method_exists($guard, 'setRequest')) {//设置request

            $guard->setRequest($this->app->refresh('request', $guard, 'setRequest'));

        }

        return $guard;

    }

最后返回的是设置了一些基础属性的SessionGuard对象


版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)

评论 (0)

    暂无评论~

njphper@copyright From 2014 to 2019-02-17