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

 更新时间:2017年09月14日 11:24:11   作者:szphper  
这篇文章主要介绍了Laravel框架用户登陆身份验证实现方法,结合实例形式分析了Laravel框架用户登陆验证的原理、实现方法与相关注意事项,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

相关文章

  • PHP实现会员注册系统

    PHP实现会员注册系统

    这篇文章主要为大家详细介绍了PHP实现会员注册系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-01-01
  • iis 7下安装laravel 5.4环境的方法教程

    iis 7下安装laravel 5.4环境的方法教程

    最近想尝试体验下laravel框架,所以自己尝试在iis 7下安装laravel 5.4环境,虽然遇到些问题,但最终都解决了,所以下面这篇文章主要给大家介绍了在iis 7下安装laravel 5.4环境的方法教程,需要的朋友可以参考下。
    2017-06-06
  • CI框架中类的自动加载问题分析

    CI框架中类的自动加载问题分析

    这篇文章主要介绍了CI框架中类的自动加载问题,结合实例形式分析了CI框架中类的自动加载功能与具体操作技巧,需要的朋友可以参考下
    2016-11-11
  • 基于php设计模式中工厂模式详细介绍

    基于php设计模式中工厂模式详细介绍

    本篇文章是对php设计模式中工厂模式进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • PHP构造二叉树算法示例

    PHP构造二叉树算法示例

    本篇文章主要介绍了PHP构造二叉树算法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • laravel5环境隐藏index.php后缀(apache)的方法

    laravel5环境隐藏index.php后缀(apache)的方法

    今天小编就为大家分享一篇laravel5环境隐藏index.php后缀(apache)的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-10-10
  • php分页原理 分页代码 分页类制作教程

    php分页原理 分页代码 分页类制作教程

    这篇文章主要为大家详细介绍了php分页原理,php分页代码,php分页类制作教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • php使用wangeditor实现富文本遇见问题及两种解决方法

    php使用wangeditor实现富文本遇见问题及两种解决方法

    在 PowerShell 中使用 npm install 命令时,命令行解析器可能会将 @ 符号解释为特殊字符,导致出现错误,遇到这样的问题如何解决呢,下面通过本文介绍php使用wangeditor实现富文本-遇见问题,需要的朋友可以参考下
    2023-12-12
  • php根据日期判断星座的函数分享

    php根据日期判断星座的函数分享

    下面提供一个用PHP来判断指定一日期属于哪个星座的函数,需要的朋友可以参考下
    2014-02-02
  • php类声明和php类使用方法示例分享

    php类声明和php类使用方法示例分享

    这篇文章主要介绍了php类声明和php类使用方法示例,需要的朋友可以参考下
    2014-03-03

最新评论