首頁 >後端開發 >php教程 >Laravel框架使用者登陸身份驗證

Laravel框架使用者登陸身份驗證

巴扎黑
巴扎黑原創
2018-05-15 09:22:481905瀏覽

這篇文章主要介紹了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框架使用者登陸身份驗證的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn