博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
yii登录
阅读量:4960 次
发布时间:2019-06-12

本文共 4110 字,大约阅读时间需要 13 分钟。

框架无非就是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(); ?>

 

 

 

转载于:https://www.cnblogs.com/jami918/archive/2013/05/03/3058449.html

你可能感兴趣的文章
04: 打开tornado源码剖析处理过程
查看>>
02: 安装epel 解决centos7无法使用yum安装nginx
查看>>
清除浮动
查看>>
PayPal(贝宝)支付接口、文档、IPN
查看>>
ORACLE 10G R2_执行计划中cost cardinality bytes cpu_cost io_cost解释
查看>>
关于this和base
查看>>
本地存储
查看>>
MP3的播放与停止
查看>>
牛客(59)按之字形顺序打印二叉树
查看>>
JavaScript 图表库 xCharts
查看>>
Android项目的目录结构
查看>>
C++中“引用”的底层实现
查看>>
vuex中的dispatch和commit
查看>>
mybatis实战教程二:多对一关联查询(一对多)
查看>>
NodeMCU文档中文翻译 3 构建固件
查看>>
前端学习☞jquery
查看>>
10分钟搞懂树状数组
查看>>
Spring Cloud与微服务构建:微服务简介
查看>>
HTTP缓存和CDN缓存
查看>>
HDU-1171 Big Event in HDU(生成函数/背包dp)
查看>>