yii做登入的方法範例:
1、LoginForm.php
使用者登陸模組,所提交的是username和password,所以我們要先建立一個Model,專門處理用戶提交的數據,所以先新建一個LoginForm.php,以下為程式碼:
<?php namespace app\modules\backend\models; use Yii; use yii\base\Model; /** * LoginForm is the model behind the login form. */ class LoginForm extends Model { public $username; public $password; public $rememberMe = true; private $_user = false; /** * @return array the validation rules. */ public function rules()<span style="white-space:pre"> </span>//① { return [ // username and password are both required [['username', 'password'], 'required','message'=>""], // rememberMe must be a boolean value ['rememberMe', 'boolean'], // password is validated by validatePassword() ['password', 'validatePassword'], ]; } /** * Validates the password. * This method serves as the inline validation for password. * * @param string $attribute the attribute currently being validated * @param array $params the additional name-value pairs given in the rule */ public function validatePassword($attribute, $params) { if (!$this->hasErrors()) { $user = $this->getUser(); if (!$user || !$user->validatePassword($this->password)) { $this->addError($attribute, 'Incorrect username or password.'); } } } /** * Logs in a user using the provided username and password. * @return boolean whether the user is logged in successfully */ public function login() { if ($this->validate()) { if($this->rememberMe) { $this->_user->generateAuthKey();//③ } return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0); } return false; } /** * Finds user by [[username]] * * @return User|null */ public function getUser() { if ($this->_user === false) { $this->_user = User::findByUsername($this->username); //② } return $this->_user; } }
該Model是根據basic模板自帶的LoginForm修改而成,程式碼中大多有註釋,這裡關注以下程式碼:
①號程式碼處是rules規則,rules規則定義了填滿過來的資料的規則,驗證所填的資料是否為空,是否符合格式之類的,其中有一欄位是password,對應的規則是validatePassword,會自動呼叫目前類別的validatePassword()方法,注意與下文的User類別對應的方法區分。
②號程式碼,呼叫了User類別裡面的findByUsername方法,這個User類別下面會寫到,主要是為了傳回一個AR類別實例,與目前LoginForm的資料進行比較。
③號代碼,這裡暫時不提,等講到cookie登陸的時候再提。
2、User.php
(1)ActiveRecord 類別
#在完成LoginForm後,我們還缺少一些東西,從使用者接受到資料了,那麼還需要從資料庫取出相應的數據來進行比較,所以我們接下來需要完成的是一個從數據庫獲取的數據的類——AR類,全稱是ActiveRecord,活動記錄類,方便用於查找數據,只要類名和數據表的表名相同,那麼它就能從這個數據表中獲取數據,比如說這樣:
<?php namespace app\modules\backend\models; use yii\db\ActiveRecord; class User extends ActiveRecord{ } ?>
還能自己添加返回的表名,只要在這個類別中重寫以下方法:
public static function tableName(){ return 'user'; }
(2)IdentityInterface 介面
一般來說,從資料庫中找到數據,只需要繼承AR類別即可,但是,我們這個是使用者登入模型,核心是驗證,所以自然需要實現核心的驗證功能,就像LoginForm模型提到的validatePassword一樣,實際的驗證邏輯是在目前的User模型完成的。一般來說,實作IdentityInterface接口,需要實作以下方法:
public static function findIdentity($id); //① public static function findIdentityByAccessToken($token, $type = null); //② public function getId(); //③ public function getAuthKey(); //④ public function validateAuthKey($authKey); //⑤
①findIdentity:是根據id查找資料表對應的資料
②findIdentityByAccessToken是根據AccessToken(上文提到的)來尋找對應的數據,而AccessToken我們在數據表也有這個字段,那麼它到底有什麼用呢?其實AccessToken在我們目前的用戶登陸模型中用處並不大,它是專門用於Resetful登陸驗證用到的,具體可自行百度,這裡不展開說明。
③getId:傳回目前AR類別所對應的id
④getAuthKey:傳回目前AR類別所對應的auth_key
⑤validateAuthKey:這個方法比較重要,是我們後面要講到的cookie登陸驗證的核心所在。
在我們的User.php實作接口,然後重寫以上方法,完整的User.php的程式碼如下:
$token]); } public static function findByUsername($username){ //① return static::findOne(['username'=>$username]); } public function getId(){ return $this->id; } public function getAuthkey(){ return $this->auth_key; } public function validateAuthKey($authKey){ return $this->auth_key === $authKey; } public function validatePassword($password){ //② return $this->password === md5($password); } /** * Generates "remember me" authentication key */ public function generateAuthKey() //③ { $this->auth_key = \Yii::$app->security->generateRandomString(); $this->save(); } } ?>
①findByUsername():在LoginForm的程式碼中,引用了這個方法,目的是根據使用者提交的username傳回一個在資料表與username相同的資料項,即AR實例。
②validatePassword():這裡將使用者提交的密碼以及目前AR類別的密碼進行比較。
③generateAuthKey():產生隨機的auth_key,用於cookie登陸。
總共寫了兩個Model類別:LoginForm和User,一個用來接收使用者提交的數據,一個用來取得資料庫的資料。
控制器(Controller)
控制器,主要用於資料的提交,把使用者提交的資料填入對應的模型(Model)中,然後根據模型傳回的資訊進一步渲染視圖(View),或執行其他邏輯。
這裡,把控制器命名為LoginController.php,以下是完整的實作程式碼:
<?php namespace app\controllers; use Yii; use yii\filters\AccessControl; use yii\web\Controller; use yii\filters\VerbFilter; use app\models\LoginForm; use app\models\ContactForm; class SiteController extends Controller { public function actionIndex() { return $this->render('index'); } public function actionLogin() { if (!\Yii::$app->user->isGuest) { //① return $this->goHome(); } $model = new LoginForm(); //② if ($model->load(Yii::$app->request->post()) && $model->login()) { //③ return $this->goBack(); //④ } return $this->render('login', [ //⑤ 'model' => $model, ]); } public function actionLogout() { Yii::$app->user->logout(); return $this->goHome(); } }
①先從\Yii::$app->user->isGuest中判斷,目前是否為遊客模式,即未登陸狀態,如果使用者已經登陸,會在user類別中儲存目前登陸使用者的資訊。
②如果目前是遊客,會先實例化一個LoginForm模型
③這行程式碼是整個login方法的核心所在,首先:$model->load(Yii::$ app->request->post())把post過來的資料填入$model,即LoginForm模型,如果回傳true,則填入成功。接著:$model->login():執行LoginForm類別裡面的login()方法,可以從login()方法裡面看到,將會執行一系列的驗證。
視圖(View)
在實作了model和controller,接下來是視圖部分,由於使用者需要輸入數據,所以我們要提供一個表單,在Yii2中,提供了ActiveForm快速產生表單,程式碼如下:
<?php /* @var $this yii\web\View */ /* @var $form yii\bootstrap\ActiveForm */ /* @var $model app\models\LoginForm */ use yii\helpers\Html; use yii\bootstrap\ActiveForm; $this->title = 'Login'; $this->params['breadcrumbs'][] = $this->title; ?> <div class="site-login"> <h1><?= Html::encode($this->title) ?></h1> <p>Please fill out the following fields to login:</p> <?php $form = ActiveForm::begin([ 'id' => 'login-form', 'options' => ['class' => 'form-horizontal'], 'fieldConfig' => [ 'template' => "{label}\n<div class=\"col-lg-3\">{input}</div>\n<div class=\"col-lg-8\">{error}</div>", 'labelOptions' => ['class' => 'col-lg-1 control-label'], ], ]); ?> <?= $form->field($model, 'username')->textInput(['autofocus' => true]) ?> <?= $form->field($model, 'password')->passwordInput() ?> <?= $form->field($model, 'rememberMe')->checkbox([ 'template' => "<div class=\"col-lg-offset-1 col-lg-3\">{input} {label}</div>\n<div class=\"col-lg-8\">{error}</div>", ]) ?> <div class="form-group"> <div class="col-lg-offset-1 col-lg-11"> <?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?> </div> </div> <?php ActiveForm::end(); ?> <div class="col-lg-offset-1" style="color:#999;"> You may login with <strong>admin/admin</strong> or <strong>demo/demo</strong>.<br> To modify the username/password, please check out the code <code>app\models\User::$users</code>. </div> </div>
推薦學習:yii框架
#以上是yii怎麼做登入的詳細內容。更多資訊請關注PHP中文網其他相關文章!

yiiremainspularbutislessfavoredthanlaravel,withabout14kgithubstars.itexcelsinperformanceandactiverecord,buthasasteperlearningcurveandasmallerecosystem.it'sidealfordealfordealfordEvelforkerfordEvelforkerplovelfordEvelforkerporporporporporporporporizatized efferporization effervastecoseposevastecosystecystemystem。

Yii是一個高性能的PHP框架,其獨特之處在於組件化架構、強大的ORM和出色的安全性。 1.組件化架構讓開發者能靈活拼裝功能。 2.強大的ORM簡化了數據操作。 3.內置多種安全功能,確保應用安全。

Yii框架採用MVC架構,並通過組件、模塊等增強其靈活性和擴展性。 1)MVC模式將應用邏輯分為模型、視圖和控制器。 2)Yii的MVC實現通過動作細化請求處理。 3)Yii支持模塊化開發,提升代碼組織和管理。 4)使用緩存和數據庫查詢優化可提升性能。

提升Yii2.0应用性能的策略包括:1.数据库查询优化,使用QueryBuilder和ActiveRecord选择特定字段和限制结果集;2.缓存策略,合理使用数据、查询和页面缓存;3.代码级优化,减少对象创建和使用高效算法。通过这些方法,可以显著提升Yii2.0应用的性能。

在Yii框架中開發RESTfulAPI可以通過以下步驟實現:定義控制器:使用yii\rest\ActiveController來定義資源控制器,如UserController。配置認證:通過添加HTTPBearer認證機制來確保API的安全性。實現分頁和排序:使用yii\data\ActiveDataProvider來處理複雜的業務邏輯。錯誤處理:配置yii\web\ErrorHandler來定制錯誤響應,如認證失敗時的處理。性能優化:利用Yii的緩存機制來優化頻繁訪問的資源,提高API性能。

在Yii框架中,組件是可重用的對象,擴展是通過Composer添加的插件。 1.組件通過配置文件或代碼實例化,使用依賴注入容器提高靈活性和可測試性。 2.擴展通過Composer管理,快速增強應用功能。使用這些工具可以提升開發效率和應用性能。

Yii框架的Theming和Templating通過主題目錄和視圖、佈局文件實現網站風格和內容生成:1.Theming通過設置主題目錄管理網站樣式和佈局,2.Templating通過視圖和佈局文件生成HTML內容,3.使用Widget系統嵌入複雜UI組件,4.優化性能和遵循最佳實踐提升用戶體驗和開發效率。

在準備Yii框架的面試時,你需要了解以下關鍵知識點:1.MVC架構:理解模型、視圖和控制器的協同工作。 2.ActiveRecord:掌握ORM工具的使用,簡化數據庫操作。 3.Widgets和Helpers:熟悉內置組件和輔助函數,快速構建用戶界面。掌握這些核心概念和最佳實踐將幫助你在面試中脫穎而出。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

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

WebStorm Mac版
好用的JavaScript開發工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3 Linux新版
SublimeText3 Linux最新版

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