成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導航首頁 ? 技術教程 ? Laravel框架用戶登陸身份驗證實現方法詳解
全站頭部文字 我要出現在這里
Laravel框架用戶登陸身份驗證實現方法詳解 815 2023-12-10   

本文實例講述了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);
});

那為什么最終會調到哪里呢,看下堆棧:

IlluminateSupportFacadesAuth::guest()
IlluminateSupportFacadesFacade::__callStatic
IlluminateAuthAuthManager->guest()
IlluminateSupportManager->__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程序設計有所幫助。



主站蜘蛛池模板: 阴阳界 电影| angela white电影| 拔萝卜视频免费播放| 一野| 施耐得| 吴青芸| 2018年党课主题及内容| va视频在线| 二年级数学下册期末测试卷| 敦煌夜谭在线观看| 美网直播| 古诗改编版搞笑大全| 吴爱玲| 生活片爱情电影大全| 四川影视文艺频道| 爱欲1990未删减版播放| 控制点电影| 24点数学题目100道| 桃乃木香奈 在线| 吻胸亲乳激情大尺度| 冥界警局| 康熙微服记四部免费观看在线| 电影电车| 教师政治学习笔记| 美女污视频| 郑丽身高一米几| 美丽人生在线完整版免费观看| 大红枣儿甜又香简谱| 你在想什么| 极乐玩偶 (1981)| 如果云知道歌词| 艳女十八式无删减版| 巨乳姐妹| 1998年槟榔西施| 长靴靴虐视频vk| 肖红| 冲出堕落城完整高清版| 大石桥联盟| 秀人网模特安然maleah简介| 丰满美女| 失魂家族|

!!!站長長期在線接!!!

網站、小程序:定制開發/二次開發/仿制開發等

各種疑難雜癥解決/定制接口/定制采集等

站長微信:lxwl520520

站長QQ:1737366103