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

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 appmodels;

use yiidbActiveRecord;
use yiiwebIdentityInterface;

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 appmodels;

use Yii;
use yiiaseModel;

//加上这一句,引用
use appmodelsMysqlUser;

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 yiihelpersHtml;
use yiiwidgetsActiveForm;
?>
<?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 appcontrollers;

use Yii;
use yiifiltersAccessControl;
use yiiwebController;
use yiifiltersVerbFilter;


//引用
use appmodelsMloginForm;

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



效果例如以下所看到的


点击登陆button后


若账号password不对


自己主动登陆还有点问题,等之后解决。


转载请注明出处:http://blog.csdn.net/zhyoulun/article/details/40687545

原文地址:https://www.cnblogs.com/mfrbuaa/p/4286458.html