Yii Framework2.0开发教程(10)配合mysql数据库实现用户登录

jerry Yii2 2015年11月23日 收藏

1、首先在mysql创建一个存用户的表格

  1. create table test_user
  2. (
  3. user_id bigint(20) unsigned not null auto_increment comment 'ID',
  4. user_email varchar(100) not null comment '电子邮件',
  5. user_password varchar(100) not null comment '密码',
  6. user_access_token varchar(200) comment 'access_token',
  7. user_auth_key varchar(200) comment 'auth_key',
  8. user_create_time datetime comment '创建时间',
  9. primary key(user_id)
  10. );

2、在表中插入一条登陆的账号


3、新建模型models/MysqlUser.php

  1. <?php
  2.  
  3. namespace app\models;
  4.  
  5. use yii\db\ActiveRecord;
  6. use yii\web\IdentityInterface;
  7.  
  8. class MysqlUser extends ActiveRecord implements IdentityInterface
  9. {
  10. public static function tableName()
  11. {
  12. //对应的表名
  13. return 'test_user';
  14. }
  15. public static function findIdentity($id)
  16. {
  17. //自动登陆时会调用
  18. $temp = parent::find()->where(['user_id'=>$id])->one();
  19. return isset($temp)?new static($temp):null;
  20. }
  21. public static function findIdentityByAccessToken($token, $type = null)
  22. {
  23. return static::findOne(['user_access_token' => $token]);
  24. }
  25. public function getId()
  26. {
  27. return $this->user_id;
  28. }
  29. public function getAuthKey()
  30. {
  31. return $this->user_auth_key;
  32. }
  33. public function validateAuthKey($authKey)
  34. {
  35. return $this->user_auth_key === $authKey;
  36. }
  37. public function validatePassword($password)
  38. {
  39. return $this->user_password === $password;
  40. }
  41. }

4、新建模型models/MloginForm.php

  1. <?php
  2.  
  3. namespace app\models;
  4.  
  5. use Yii;
  6. use yii\base\Model;
  7.  
  8. //加上这一句,引用
  9. use app\models\MysqlUser;
  10.  
  11. class MloginForm extends Model
  12. {
  13. public $email;
  14. public $password;
  15. private $_user = false;
  16. public function rules()
  17. {
  18. return [
  19. ['email','email','message'=>'必须是邮件格式'],
  20. [['email','password'],'required','message'=>'必填'],
  21. ['password','validatePassword','message'=>'账号或密码不正确'],
  22. ];
  23. }
  24. //登陆
  25. public function login()
  26. {
  27. if ($this->validate())
  28. return Yii::$app->user->login($this->getUser(), 3600*24*30);
  29. else
  30. return false;
  31. }
  32. //判断账号密码是否正确
  33. public function validatePassword($attribute, $params)
  34. {
  35. if (!$this->hasErrors()) 
  36. {
  37. $user = $this->getUser();
  38. if (!$user)
  39. {
  40.        $this->addError($attribute, '账号或密码不正确');
  41.       }
  42. }
  43. }
  44. //根据邮箱和密码查询数据库
  45. public function getUser()
  46. {
  47. if ($this->_user === false)
  48. {
  49. $this->_user = MysqlUser::find()->where(['user_email'=>$this->email,'user_password'=>$this->password])->one();
  50. }
  51. return $this->_user;
  52. }
  53. }
  54.  
  55. ?>

5、新建视图views/account/login.php

  1. <?php
  2. use yii\helpers\Html;
  3. use yii\widgets\ActiveForm;
  4. ?>
  5. <?php
  6. echo '<h1>'.$status.'</h1>';
  7. ?>
  8. <?php $form = ActiveForm::begin(); ?>
  9. <?php echo $form->field($model, 'email'); ?>
  10. <?php echo $form->field($model, 'password'); ?>
  11. <?php echo Html::submitButton('登陆'); ?>
  12. <?php ActiveForm::end(); ?>

6、新建控制器controllers/AccountController.php

  1. <?php
  2. namespace app\controllers;
  3.  
  4. use Yii;
  5. use yii\filters\AccessControl;
  6. use yii\web\Controller;
  7. use yii\filters\VerbFilter;
  8.  
  9.  
  10. //引用
  11. use app\models\MloginForm;
  12.  
  13. class AccountController extends Controller
  14. {
  15. function actionLogin()
  16. {
  17. $model = new MloginForm();
  18. if($model->load(Yii::$app->request->post()))
  19. {
  20. if($model->login())
  21. //登陆成功
  22. return $this->renderPartial('login',['model'=>$model,'status'=>'成功']);
  23. else
  24. //登陆失败
  25. return $this->renderPartial('login',['model'=>$model,'status'=>'失败']);
  26. }
  27. else
  28. {
  29. return $this->renderPartial('login',['model'=>$model,'status'=>'']);
  30. }
  31. }
  32. }

另外,自动登陆配置的地方是config/web.php


效果如下所示


点击登陆按钮后


若账号密码不正确