Laravel框架使用者登陸身份驗證
這篇文章主要介紹了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中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

使用ORM可簡化PHP中的資料庫操作,它將物件對應到關聯式資料庫中。 Laravel中的EloquentORM允許使用物件導向的語法與資料庫交互,可透過定義模型類別、使用Eloquent方法或在實戰中建立部落格系統等方式來使用ORM。

Laravel9和CodeIgniter4的最新版本提供了更新的功能和改進。 Laravel9採用MVC架構,提供資料庫遷移、驗證及模板引擎等功能。 CodeIgniter4採用HMVC架構,提供路由、ORM和快取。在性能方面,Laravel9的基於服務提供者設計模式和CodeIgniter4的輕量級框架使其具有出色的性能。在實際應用中,Laravel9適用於需要靈活性和強大功能的複雜項目,而CodeIgniter4適用於快速開發和小型應用程式。

Laravel - Artisan 指令 - Laravel 5.7 提供了處理和測試新指令的新方法。它包括測試 artisan 命令的新功能,下面提到了演示?

比較Laravel和CodeIgniter的資料處理能力:ORM:Laravel使用EloquentORM,提供類別物件關係映射,而CodeIgniter使用ActiveRecord,將資料庫模型表示為PHP類別的子類別。查詢建構器:Laravel具有靈活的鍊式查詢API,而CodeIgniter的查詢建構器更簡單,基於陣列。資料驗證:Laravel提供了一個Validator類,支援自訂驗證規則,而CodeIgniter的驗證功能內建較少,需要手動編碼自訂規則。實戰案例:用戶註冊範例展示了Lar

對於初學者來說,CodeIgniter的學習曲線更平緩,功能較少,但涵蓋了基本需求。 Laravel提供了更廣泛的功能集,但學習曲線稍陡。在性能方面,Laravel和CodeIgniter都表現出色。 Laravel有更廣泛的文件和活躍的社群支持,而CodeIgniter更簡單、輕量級,具有強大的安全功能。在建立部落格應用程式的實戰案例中,Laravel的EloquentORM簡化了資料操作,而CodeIgniter需要更多的手動配置。

在選擇大型專案框架時,Laravel和CodeIgniter各有優勢。 Laravel針對企業級應用程式而設計,提供模組化設計、相依性注入和強大的功能集。 CodeIgniter是一款輕量級框架,更適合小型到中型項目,強調速度和易用性。對於具有複雜需求和大量用戶的大型項目,Laravel的強大功能和可擴展性更為合適。而對於簡單專案或資源有限的情況下,CodeIgniter的輕量級和快速開發能力則較為理想。

PHP單元和整合測試指南單元測試:專注於單一程式碼單元或函數,使用PHPUnit建立測試案例類別進行驗證。整合測試:專注於多個程式碼單元協同工作的情況,使用PHPUnit的setUp()和tearDown()方法設定和清理測試環境。實戰案例:使用PHPUnit在Laravel應用程式中進行單元和整合測試,包括建立資料庫、啟動伺服器以及編寫測試程式碼。

對於小型項目,Laravel適用於大型項目,需要強大的功能和安全性。 CodeIgniter適用於非常小的項目,需要輕量級和易用性。
