框架无非就是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模型
_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
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视图
beginWidget('CActiveForm', array( 'id'=>'login-form', 'enableClientValidation'=>true, 'clientOptions'=>array( 'validateOnSubmit'=>true, ),)); ?> errorSummary($model); ?>label($model,'username'); ?> textField($model,'username') ?> error($model,'username'); ?>label($model,'password'); ?> passwordField($model,'password') ?> error($model,'password'); ?>checkBox($model,'rememberMe'); ?> label($model,'rememberMe'); ?> error($model,'rememberMe'); ?>'btn btn-large')); ?>endWidget(); ?>