1、首先在mysql创建一个存用户的表格
create table test_user ( user_id bigint(20) unsigned not null auto_increment comment 'ID', user_email varchar(100) not null comment '电子邮件', user_password varchar(100) not null comment '密码', user_access_token varchar(200) comment 'access_token', user_auth_key varchar(200) comment 'auth_key', user_create_time datetime comment '创建时间', primary key(user_id) );
2、在表中插入一条登陆的账号
3、新建模型models/MysqlUser.php
<?php namespace app\models; use yii\db\ActiveRecord; use yii\web\IdentityInterface; class MysqlUser extends ActiveRecord implements IdentityInterface { public static function tableName() { //对应的表名 return 'test_user'; } public static function findIdentity($id) { //自动登陆时会调用 $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、新建模型models/MloginForm.php
<?php namespace app\models; use Yii; use yii\base\Model; //加上这一句,引用 use app\models\MysqlUser; class MloginForm extends Model { public $email; public $password; private $_user = false; public function rules() { return [ ['email','email','message'=>'必须是邮件格式'], [['email','password'],'required','message'=>'必填'], ['password','validatePassword','message'=>'账号或密码不正确'], ]; } //登陆 public function login() { if ($this->validate()) return Yii::$app->user->login($this->getUser(), 3600*24*30); else return false; } //判断账号密码是否正确 public function validatePassword($attribute, $params) { if (!$this->hasErrors()) { $user = $this->getUser(); if (!$user) { $this->addError($attribute, '账号或密码不正确'); } } } //根据邮箱和密码查询数据库 public function getUser() { if ($this->_user === false) { $this->_user = MysqlUser::find()->where(['user_email'=>$this->email,'user_password'=>$this->password])->one(); } return $this->_user; } } ?>
5、新建视图views/account/login.php
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; ?> <?php echo '<h1>'.$status.'</h1>'; ?> <?php $form = ActiveForm::begin(); ?> <?php echo $form->field($model, 'email'); ?> <?php echo $form->field($model, 'password'); ?> <?php echo Html::submitButton('登陆'); ?> <?php ActiveForm::end(); ?>
6、新建控制器controllers/AccountController.php
<?php namespace app\controllers; use Yii; use yii\filters\AccessControl; use yii\web\Controller; use yii\filters\VerbFilter; //引用 use app\models\MloginForm; class AccountController extends Controller { function actionLogin() { $model = new MloginForm(); if($model->load(Yii::$app->request->post())) { if($model->login()) //登陆成功 return $this->renderPartial('login',['model'=>$model,'status'=>'成功']); else //登陆失败 return $this->renderPartial('login',['model'=>$model,'status'=>'失败']); } else { return $this->renderPartial('login',['model'=>$model,'status'=>'']); } } }
另外,自动登陆配置的地方是config/web.php
效果如下所示
点击登陆按钮后
若账号密码不正确