Laravel框架用戶登陸身份驗(yàn)證實(shí)現(xiàn)方法詳解
本文實(shí)例講述了Laravel框架用戶登陸身份驗(yàn)證實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
laravel中檢測(cè)用戶是否登錄,有以下的代碼:
if ( !Auth::guest() ) { return Redirect::to('/dashboard'); }
那Auth::guest
是如何調(diào)用的呢?
laravel用了Facade模式,相關(guān)門(mén)面類(lèi)在laravel/framework/src/Illuminate/Support/Facades文件夾定義的,看下Auth類(lèi)的定義:
class Auth extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'auth'; } }
laravel框架中,F(xiàn)acade模式使用反射,相關(guān)方法其實(shí)調(diào)用app['auth']中的方法,app['auth']是什么時(shí)候創(chuàng)建的呢,
AuthServiceProvider::register
方法會(huì)注冊(cè):
$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); });
那為什么最終會(huì)調(diào)到哪里呢,看下堆棧:
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]; }
沒(méi)有會(huì)調(diào)用getDefaultDrive方法
/** * Get the default authentication driver name. * * @return string */ public function getDefaultDriver() { return $this->app['config']['auth.driver']; }
最終調(diào)用的是配置文件中配置的driver,如果配的是
'driver' => 'eloquent'
則調(diào)用的是
public function createEloquentDriver() { $provider = $this->createEloquentProvider(); return new Guard($provider, $this->app['session.store']); }
所以Auth::guest
最終調(diào)用的是Guard::guest
方法
這里的邏輯先從session中取用戶信息,奇怪的是session里只保存的是用戶ID,然后拿這個(gè)ID來(lái)從數(shù)據(jù)庫(kù)中取用戶信息
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; }
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Laravel框架入門(mén)與進(jìn)階教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
- Laravel實(shí)現(xiàn)用戶注冊(cè)和登錄
- Laravel 5框架學(xué)習(xí)之用戶認(rèn)證
- Laravel重寫(xiě)用戶登錄簡(jiǎn)單示例
- Laravel實(shí)現(xiàn)用戶多字段認(rèn)證的解決方法
- Laravel5.5 實(shí)現(xiàn)后臺(tái)管理登錄的方法(自定義用戶表登錄)
- 解決laravel5中auth用戶登錄其他頁(yè)面獲取不到登錄信息的問(wèn)題
- laravel-admin 管理平臺(tái)獲取當(dāng)前登陸用戶信息的例子
- Laravel多用戶認(rèn)證系統(tǒng)示例詳解
- Laravel6.2中用于用戶登錄的新密碼確認(rèn)流程詳解
- Laravel 微信小程序后端實(shí)現(xiàn)用戶登錄的示例代碼
- Laravel用戶授權(quán)系統(tǒng)的使用方法示例
- laravel利用中間件防止未登錄用戶直接訪問(wèn)后臺(tái)的方法
- Laravel 框架基于自帶的用戶系統(tǒng)實(shí)現(xiàn)登錄注冊(cè)及錯(cuò)誤處理功能分析
相關(guān)文章
默默小談PHP&MYSQL分頁(yè)原理及實(shí)現(xiàn)
默默小談PHP&MYSQL分頁(yè)原理及實(shí)現(xiàn)...2007-01-01PHP設(shè)計(jì)模式之適配器模式代碼實(shí)例
這篇文章主要介紹了PHP設(shè)計(jì)模式之適配器模式代碼實(shí)例,本文講解了目標(biāo)、角色、應(yīng)用場(chǎng)景、優(yōu)勢(shì)等內(nèi)容,并給出代碼實(shí)例,需要的朋友可以參考下2015-05-05PHP實(shí)現(xiàn)即時(shí)輸出、實(shí)時(shí)輸出內(nèi)容方法
這篇文章主要介紹了PHP實(shí)現(xiàn)即時(shí)輸出、實(shí)時(shí)輸出內(nèi)容方法,本文直接給出實(shí)現(xiàn)方法,需要的朋友可以參考下2015-05-05PHP驗(yàn)證信用卡卡號(hào)是否正確函數(shù)
這篇文章主要介紹了PHP驗(yàn)證信用卡卡號(hào)是否正確函數(shù),本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-05-05Laravel5.5 視圖 - 創(chuàng)建視圖和數(shù)據(jù)傳遞示例
今天小編就為大家分享一篇Laravel5.5 視圖 - 創(chuàng)建視圖和數(shù)據(jù)傳遞示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10