欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Laravel框架用户登陆身份验证实现方法详解

程序员文章站 2022-06-22 20:52:54
本文实例讲述了laravel框架用户登陆身份验证实现方法。分享给大家供大家参考,具体如下: laravel中检测用户是否登录,有以下的代码: if ( !aut...

本文实例讲述了laravel框架用户登陆身份验证实现方法。分享给大家供大家参考,具体如下:

laravel中检测用户是否登录,有以下的代码:

if ( !auth::guest() )
{
  return redirect::to('/dashboard');
}

auth::guest是如何调用的呢?

laravel用了facade模式,相关门面类在laravel/framework/src/illuminate/support/facades文件夹定义的,看下auth类的定义:

class auth extends facade {
  /**
   * get the registered name of the component.
   *
   * @return string
   */
  protected static function getfacadeaccessor() { return 'auth'; }
}

laravel框架中,facade模式使用反射,相关方法其实调用app['auth']中的方法,app['auth']是什么时候创建的呢,

authserviceprovider::register方法会注册:

$this->app->bindshared('auth', function($app)
{
  // once the authentication service has actually been requested by the developer
  // we will set a variable in the application indicating such. this helps us
  // know that we need to set any queued cookies in the after event later.
  $app['auth.loaded'] = true;
  return new authmanager($app);
});

那为什么最终会调到哪里呢,看下堆栈:

illuminate\support\facades\auth::guest()
illuminate\support\facades\facade::__callstatic
illuminate\auth\authmanager->guest()
illuminate\support\manager->__call
public function __call($method, $parameters)
{
    return call_user_func_array(array($this->driver(), $method), $parameters);
}

看下driver的代码:

public function driver($driver = null)
{
    $driver = $driver ?: $this->getdefaultdriver();
    // if the given driver has not been created before, we will create the instances
    // here and cache it so we can return it next time very quickly. if there is
    // already a driver created by this name, we'll just return that instance.
    if ( ! isset($this->drivers[$driver]))
    {
      $this->drivers[$driver] = $this->createdriver($driver);
    }
    return $this->drivers[$driver];
}

没有会调用getdefaultdrive方法

/**
* get the default authentication driver name.
*
* @return string
*/
public function getdefaultdriver()
{
    return $this->app['config']['auth.driver'];
}

最终调用的是配置文件中配置的driver,如果配的是

'driver' => 'eloquent'

则调用的是

public function createeloquentdriver()
{
    $provider = $this->createeloquentprovider();
    return new guard($provider, $this->app['session.store']);
}

所以auth::guest最终调用的是guard::guest方法

这里的逻辑先从session中取用户信息,奇怪的是session里只保存的是用户id,然后拿这个id来从数据库中取用户信息

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))
    {
      //provider为eloquentuserprovider
     $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);
    }
    return $this->user = $user;
}

更多关于laravel相关内容感兴趣的读者可查看本站专题:《laravel框架入门与进阶教程》、《php优秀开发框架总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家基于laravel框架的php程序设计有所帮助。