class LoginForm extends CFormModel {}表单的模型要继承CFormModel
controller
$model=new LoginForm;controller 实例化要用到的表单,传递给views$this->render('login',array('model'=>$model));
views
views用 beginWidget('CActiveForm')生成一个表单<?php $form=$this->beginWidget('CActiveForm', array('id'=>'login-form','enableAjaxValidation'=>true,)); ?>
....
<?php $this->endWidget(); ?>
填入表单元素
解释:<div class="form"> <?php $form=$this->beginWidget('CActiveForm', array('id'=>'login-form','enableAjaxValidation'=>true,)); ?> <p class="note">Fields with <span class="required">*</span> are required.</p> <div class="row"> <?php echo $form->labelEx($model,'username'); ?> <?php echo $form->textField($model,'username'); ?> <?php echo $form->error($model,'username'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'password'); ?> <?php echo $form->passwordField($model,'password'); ?> <?php echo $form->error($model,'password'); ?> <p class="hint"> Hint: You may login with <tt>demo/demo</tt>. </p> </div> <div class="row rememberMe"> <?php echo $form->checkBox($model,'rememberMe'); ?> <?php echo $form->label($model,'rememberMe'); ?> <?php echo $form->error($model,'rememberMe'); ?> </div> <div class="row submit"> <?php echo CHtml::submitButton('Login'); ?> </div> <?php $this->endWidget(); ?> </div><!-- form -->
输出model里各元素的配置值<?php echo $form->labelEx($model,'username'); ?>
labelEx 指输出 model的username的扩展配置值
label 指输出 model的username的配置值
$model 中username的配置的部分
views中public function rules() { return array( array('username, password', 'required'), array('rememberMe', 'boolean'), array('password', 'authenticate'), ); }
public function attributeLabels() { return array('rememberMe'=>'Remember me next time','username'=>'this is test',); }
对应model的配置值为:<?php echo $form->error($model,'password'); ?>
public function authenticate($attribute,$params) { $this->_identity=new UserIdentity($this->username,$this->password); if(!$this->_identity->authenticate()) $this->addError('password','Incorrect username or password.'); }
输出结果:
<label class="required" for="LoginForm_username">this is test <span class="required">*</span></label>
echo $form->labelEx($model,'map_id');比如这个是输出 map_id 在model里配置的值,
如果我想在后面输出 数字 来输出ID1, ID2这样的情况,就只能使用下面的做法:
====================================================================================CHtml::label(($model->map_id).$i);
提交表单,处理表单
controller
$model->attributes 为setProperty()魔术方法,将表单的值赋值给 $model里相应的变量$model=new LoginForm;
if(isset($_POST['LoginForm'])) { $model->attributes=$_POST['LoginForm']; if($model->validate() && $model->login()) $this->redirect(Yii::app()->user->returnUrl); }
model中要有相应的变量(类似extract($_POST))
public $username; public $password; public $rememberMe;
验证表单
controller
if(isset($_POST['LoginForm'])) { $model->attributes=$_POST['LoginForm']; if($model->validate() && $model->login()) $this->redirect(Yii::app()->user->returnUrl); }model 中的验证规则
public function rules() { return array( array('username, password', 'required'), array('rememberMe', 'boolean'), array('password', 'authenticate'), ); }
验证用户
controller
if(isset($_POST['LoginForm'])) { $model->attributes=$_POST['LoginForm']; if($model->validate() && $model->login()) $this->redirect(Yii::app()->user->returnUrl); } $this->render('login',array('model'=>$model)); }
model
public function login() { if($this->_identity===null) { $this->_identity=new UserIdentity($this->username,$this->password); $this->_identity->authenticate(); } if($this->_identity->errorCode===UserIdentity::ERROR_NONE) { $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days Yii::app()->user->login($this->_identity,$duration); return true; }else return false; }UserIdentity 类放在components中,为了方便改变验证方法
使用YII的CUserIdentity 验证(主要是用了返回代码:ERROR_USERNAME_INVALID等)
CUserIdentity 类class UserIdentity extends CUserIdentity { private $_id; public function authenticate() { $user=User::model()->find('LOWER(username)=?',array(strtolower($this->username))); if($user===null) $this->errorCode=self::ERROR_USERNAME_INVALID; else if(!$user->validatePassword($this->password)) $this->errorCode=self::ERROR_PASSWORD_INVALID; else { $this->_id=$user->id; $this->username=$user->username; $this->errorCode=self::ERROR_NONE; } return $this->errorCode==self::ERROR_NONE; }
CBaseUserIdentity 类class CUserIdentity extends CBaseUserIdentity {
public $username; public $password;
public function __construct($username,$password) { $this->username=$username; $this->password=$password; }
.......
}
abstract class CBaseUserIdentity extends CComponent implements IUserIdentity { const ERROR_NONE=0; const ERROR_USERNAME_INVALID=1; const ERROR_PASSWORD_INVALID=2; const ERROR_UNKNOWN_IDENTITY=100;
......
}
===========================================================================================
User类 显示,获得用户的信息
User是CActiveRecord 类,可以操作数据库。将数据表字段都extracts为变量
table 'tbl_user': * @var integer $id * @var string $username * @var string $password * @var string $salt * @var string $email * @var string $profile
查询数据库<?php class User extends CActiveRecord{ public static function model($className=__CLASS__) { return parent::model($className); } public function tableName() { return '{{user}}'; } }
$condition 是一条sql 的where 子句 ,格式:/ 查找满足指定条件的结果中的第一行 $user=User::model()->find($condition,$params); // 查找具有指定主键值的那一行 $user=User::model()->findByPk($userID,$condition,$params); // 查找具有指定属性值的行 $user=User::model()->findByAttributes($attributes,$condition,$params); // 通过指定的 SQL 语句查找结果中的第一行 $user=User::model()->findBySql($sql,$params);
=====================================================================find('LOWER(username)=?',array(strtolower($this->username)))
find('postID=:postID', array(':postID'=>10));
find('id = :id AND title = :title', array(':id' => 1,':title' => '标题',));
密码验证:
use 模型
public function validatePassword($password) { return $this->hashPassword($password,$this->salt)===$this->password; }
public function hashPassword($password,$salt) { return md5($salt.$password); }
图示:
正确的话:
model记录用户
controller返回前一URLpublic function login() { if($this->_identity===null) { $this->_identity=new UserIdentity($this->username,$this->password); $this->_identity->authenticate(); } if($this->_identity->errorCode===UserIdentity::ERROR_NONE) { $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days Yii::app()->user->login($this->_identity,$duration); return true; } else return false; }
if(isset($_POST['LoginForm'])) { $model->attributes=$_POST['LoginForm']; if($model->validate() && $model->login()) $this->redirect(Yii::app()->user->returnUrl); }
错误时处理
controller
$model->validate() 方法验证 rules()规定的规则if(isset($_POST['LoginForm'])) { $model->attributes=$_POST['LoginForm']; if($model->validate() && $model->login()) $this->redirect(Yii::app()->user->returnUrl); }
model
public function rules() { return array( array('username, password', 'required'), array('rememberMe', 'boolean'), array('password', 'authenticate'), ); }
public function authenticate($attribute,$params) { $this->_identity=new UserIdentity($this->username,$this->password); if(!$this->_identity->authenticate()) $this->addError('password','Incorrect username or password.'); }