1. First create a user-saved form in MySQL
CREATE TABLE Test_user (user_id bigint () unsigned not null auto_increment comment ' id ', user_email varchar (+) NOT NULL C Omment ' email ', user_password varchar (+) NOT null comment ' password ', user_access_token varchar comment ' Access_token ', User_auth_key varchar (comment ' Auth_key ', user_create_time datetime comment ' creation time ', primary key (USER_ID));
2. Insert a login account in the table
3. New Model models/mysqluser.php
<?phpnamespace app\models;use yii\db\activerecord;use Yii\web\identityinterface;class MysqlUser extends ActiveRecord implements identityinterface{public static function TableName () {//corresponding table name return ' Test_user ';} public static function Findidentity ($id) {//auto-Login will call $temp = Parent::find ()->where ([' user_id ' = ' $id])->one (); return Isset ($temp)? New static ($temp): null;} public static function Findidentitybyaccesstoken ($token, $type = null) {return static::findone ([' User_access_token ' = > $token]);} Public Function GetId () {return $this->user_id;} Public Function Getauthkey () {return $this->user_auth_key;} Public Function Validateauthkey ($authKey) {return $this->user_auth_key = = = $authKey;} Public Function ValidatePassword ($password) {return $this->user_password = = = $password;}}
4. New Model models/mloginform.php
<?phpnamespace app\models;use yii;use yii\base\model;//Plus this sentence, quote use App\models\mysqluser;class MloginForm extends Model{public $email;p ublic $password;p rivate $_user = false;public function Rules () {return [' email ', ' email ', ' message ' = ' must be mail format '],[[' email ', ' password ', ' Required ', ' message ' = ' Required '],[' password ', ' ValidatePassword ', ' message ' = > ' Incorrect account or password ',];} Login Public Function Login () {if ($this->validate ()) return Yii:: $app->user->login ($this->getuser (), 3600* 24*30); Elsereturn false;} Determine if the account password is correct public function ValidatePassword ($attribute, $params) {if (! $this->haserrors ()) {$user = $this GetUser (); if (! $user) {$this->adderror ($attribute, ' Incorrect account or password '); }}}//Query Database Public Function GetUser () {if ($this->_user = = = False) According to mailbox and password {$this->_user = Mysqluser::find () where ([' user_email ' = = $this->email, ' user_password ' = $this->password])->one ();} return $this->_user;}}? >
5. New View views/account/login.php
<?phpuse yii\helpers\html;use yii\widgets\activeform;? ><?phpecho '
6. New Controller controllers/accountcontroller.php
<?phpnamespace app\controllers;use yii;use yii\filters\accesscontrol;use yii\web\controller;use yii\filters\ verbfilter;//reference use App\models\mloginform;class AccountController extends Controller{function actionLogin () {$model = New Mloginform (); if ($model->load (Yii:: $app->request->post ())) {if ($model->login ())//Login successfully return $this- >renderpartial (' login ', [' model ' = $model, ' status ' = ' success ']), else//Login failed return $this->renderpartial (' Login ', [' model ' = + $model, ' status ' = ' failed ']); Else{return $this->renderpartial (' login ', [' model ' = ' + $model, ' status ' = ']);}}
In addition, the location of the automatic landing configuration is config/web.php
The effect is as follows
After clicking the login button
If the account password is not correct
Automatic landing also a bit of a problem, and so on after the solution.
Reprint Please specify source: http://blog.csdn.net/zhyoulun/article/details/40687545
Yii Framework2.0 Development Tutorials (10) Implement user login with MySQL database