在advanced
模板中,進入frontend/index.php?r=site%2Fsignup
頁面,能夠看到框架的註冊頁面
php
填寫完Username
、Email
和Password
後點擊Signup
後,若是格式不對,frontend/models/SignuForm
中的rules()
函數會進行初步驗證,全部格式正確後,數據傳輸到 frontend/controllers /SiteController
中的 actionSignup()
函數中,函數加載用戶輸入的註冊信息,在frontend/models/SignupForm
中的signup()
函數,web
如下引用的文字爲解釋函數中的具體細節,不閱讀不影響總體,由於沒有摺疊文字功能,故採用引用的方法,下同if (!$this->validate()) { return null; }
signup()
函數首先調用yii2/base/Model
中的validate()
函數進行驗證
第一步,清除使用frontend/models/SignuForm
中的rules()
函數在用戶輸入時的錯誤信息數據庫if ($clearErrors) { $this->clearErrors(); }第二步,
beforeValidate()
函數觸發beforeValidate
事件並返回true
第三步,設置scenario
,默認是default
第四步,由於這裏的$attributeNames
爲null
,windows$attributeNames = $this->activeAttributes();執行後返回yii2
array(3) { [0]=> string(8) "username" [1]=> string(5) "email" [2]=> string(8) >"password" }第五步,
$this->getActiveValidators()
會獲得frontend/models/SignuForm
中的rules()
中11條驗證規則給validateAttributes()
進行驗證
第六步,執行afterValidate()
函數觸發afterValidate
事件
最後 若是全部驗證都經過,$this->hasErrors()
爲false
,因此函數最後返回true
cookie
咱們看一下數據表user
的字段
session
用戶輸入了username
、password
和email
,Yii2
框架是如何生成其餘的字段的呢,先看password_hash
,在SignupFrom
中的signup
函數中的密碼生成是setPassword
函數,該函數在common/models/User
中,setPassword
函數調用了yii2/base/Security
中的每一條規則generatePasswordHash
函數。app
if (function_exists('password_hash')) { /** @noinspection PhpUndefinedConstantInspection */ return password_hash($password, PASSWORD_DEFAULT, ['cost' => $cost]); }
若是有,就使用password_hash
函數進行加密,若是PHP
沒有password_hash
函數,就使用crypt
函數加密,初步判斷應該是爲了兼容PHP
低於5.5的版本,畢竟大於5.5的版本纔開始有password_hash
函數框架
common/models/User
的signup()
函數在對password
加密後,就會繼續生成一個auth key
,auth key
是當用戶在登陸的時候點擊 remember me
的時候的驗證信息,frontend
auth key
生成的方法也是在yii2/base/Security
中的generateRandomString
,generateRandomString
調用generateRandomKey
函數,若是你的PHP
版本爲是5.2~5.6或者是7,那就是用random_bytes
生成一個32個字節的字符串,若是不是,當你用的系統時windows
而且安裝了OpenSSL
,就會調用openssl_random_pseudo_bytes
函數生成,若是你未安裝OpenSSL
,就會使用mcrypt_create_iv
生成。
若是你使用的系統不是windows
,就須要調用/dev/urandom
,FreeBSD
系統特殊,會調用/dev/random
,而後調用stream_set_read_buffer
方法生成8字節的字符文件,生成後,經過fread
函數讀取該文件中的32個字節,而後返回該數據。
password_reset_token
在用戶註冊的時候是爲空的,當用戶忘記密碼在登陸頁面點擊reset it
後生成的,用來給用法發送郵件後重置密碼時進行驗證。status
在common/models/User
中定義的
const STATUS_DELETED = 0; const STATUS_ACTIVE = 10;
用戶註冊時rules
中的status
默認爲爲10,created_time
和updated_time
也是在common/models/User
中的behaviors()
函數中生成
用戶的數據驗證合格,加上框架生成的數據,而後存儲進數據的user
表裏。
關於frontend/controllers/SiteController
中的actionSignup()
中的
if (Yii::$app->getUser()->login($user)) { return $this->goHome(); }
就是用戶註冊後,這時該用戶的數據已經寫入數據庫了,開始登陸的過程了
登陸的過程在yii2/web/User
裏的login()
函數中
第一步,執行beforeLogin()
函數觸發beforeLogin
事件
第二步,switchIdentity()
函數把我的信息換成當前用戶的信息,把全部的cookie
都銷燬,而後把當前用戶的信息都存入到session
和cookie
中
第三步,獲取當前用戶的id
和用戶登陸的ip
,並寫入到log
中
第四步,執行afterLogin()
函數觸發afterLogin
事件
最後 返回true
判斷登陸成功後,return $this->goHome();
跳轉到主頁。
若有錯誤,不吝賜教。