yii登录
框架无非就是mvc,只是调用的父类操作比较繁琐。
登录:
C:TestController.php/actionLogin方法
public function actionLogin()
{
$model=new TestLogin;
if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
if(isset($_POST['TestLogin']))
{
$aa=$model->attributes=$_POST['TestLogin'];
if($model->validate() && $model->login()){
$this->redirect (array('aa/test/index'));
}
//$this->redirect (yii::app()->user->rerurnUrl);
}
//$this->renderPartial('login',array('model'=>$model));
$this->render('login',array('model'=>$model));
}
M:TestLogin.php模型
<?php
class TestLogin extends CFormModel{
public $username;
public $password;
public $rememberMe = false;
private $_identity;
/**
* 用户登录,需要用户名和密码
* 验证密码
* @return array
*/
public function rules()
{
return array(
array('username,password', 'required'),
array('rememberMe','boolean'),
array('password','authenticate'),//调用自己的写的authenticate方法
);
}
public function authenticate($attribute,$params)
{
$this->_identity=new UserIdentity($this->username, $this->password);//这里实例化了一个UserIdentity组件类,并调用了User.php模型类!
if(!$this->_identity->authenticate())
{
$this->addError('password','用户名或密码错误。');
}
}
public function attributeLabels()
{
return array(
'username' => '用户名',
'password' => '密码',
'rememberMe'=>'Remember me next time',
);
}
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;
}
}
?>
加载的组件类D:\wamp\www\yiitest\protected\components\UserIdentity.php
class UserIdentity extends CUserIdentity
{
public function authenticate()
{
$users = User::model() -> findByAttributes(array('username'=>$this->username));
if(!isset($users->username))
$this->errorCode=self::ERROR_USERNAME_INVALID;
elseif($users->password!==md5($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
$this->errorCode=self::ERROR_NONE;
return !$this->errorCode;
}
}
加载的模型类D:\wamp\www\yiitest\protected\models\User.php
<?php
class User extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return User the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'tbl_user';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('username, password, email', 'required'),
array('username, password, email', 'length', 'max'=>128),
array('profile', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, username, password, email, profile', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'posts' => array(self::HAS_MANY, 'Post', 'author_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'username' => 'Username',
'password' => 'Password',
'email' => 'Email',
'profile' => 'Profile',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('username',$this->username,true);
$criteria->compare('password',$this->password,true);
$criteria->compare('email',$this->email,true);
$criteria->compare('profile',$this->profile,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
V:login.php视图
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<?php //echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->label($model,'username'); ?>
<?php echo $form->textField($model,'username') ?>
<?php echo $form->error($model,'username'); ?>
</div>
<div class="row">
<?php echo $form->label($model,'password'); ?>
<?php echo $form->passwordField($model,'password') ?>
<?php echo $form->error($model,'password'); ?>
</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('登 录',array('class'=>'btn btn-large')); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->

浙公网安备 33010602011771号