用yii1.8框架执行简单的数据库的增删改查,及部分页面配置(yii框架的下载不再讲解);
一、yii框架的开发前的基本配置:
首先在config/main.php中配置默认的controller (框架默认的controller为SiteController),我们在这里创建IndexController,切记我们的一个Controller对应一个同名字的前端页面文件夹 (同目录的views下),例如IndexController对应的是views/index的文件夹;默认的方法定义:
return array( 'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..', 'name'=>'My Web Application', // preloading 'log' component 'preload'=>array('log'), //加上这句话,就是默认 的Controller为IndexController 'defaultController'=>'Index',
然后我们可以在上述的views/index下创建index.php,然后在IndexController 直接跳转:
class IndexController extends Controller { public function actionIndex(){ $this->render('index'); } }
因为上文 已经讲述 他们是一一对应的关系,所以默认的就是views/index目录下的index.php页面;
然后我们就配置 一下数据库,就开始一个小网站demo的建立,回到 我们的 config /main.php页面,写入以下代码
'db'=>array( 'connectionString' => 'mysql:host=127.0.0.1;dbname=db_blog', 'emulatePrepare' => true, 'username' => 'root', 'password' => '', 'charset' => 'utf8', //数据库的表前缀 'tablePrefix' => 't_', //打开数据库调试 'enableParamLogging' => true ),
然后我们 就可以直接 运用yii模块 创建后台框架了,方法如下:
①首先打开主配置main.php中的gii模块
//初始状态 'modules'=>array( // uncomment the following to enable the Gii tool // 'gii'=>array( // 'class'=>'system.gii.GiiModule', // 'password'=>'123', // // If removed, Gii defaults to localhost only. Edit carefully to taste. // 'ipFilters'=>array('127.0.0.1','::1'), // ), ), //创建过后状态 'modules'=>array( // uncomment the following to enable the Gii tool // 'gii'=>array( // 'class'=>'system.gii.GiiModule', // 'password'=>'123', // // If removed, Gii defaults to localhost only. Edit carefully to taste. // 'ipFilters'=>array('127.0.0.1','::1'), // ), 'admin' ),
首先在 初始状态下,我们取消注释,设置password,登录 gii模块的时候需要使用,但是这个gii模块是不安全的,所以我们 创建过后 要关闭它;取消注释后,登录在自己的默认主页路径下+?r=gii,例如http://localhost/yii/blog/index.php?r=gii;这时候输入 自己定义的密码,然后进入首页选择最下面的模块,一路默认,最后要输入自己的ID,我输入的 ID名字为admin,而admin是 一个模块,所以要在 主配置文件中配置一下,就有了我创建 过后的那个代码状态;
再然后 我们就可以愉快的 写代码了;
二、登录实现
进入 刚才我们创建的后台模块;老规矩首先创建我们的LoginController及所对应前端页面,把登录页面的样式写好:
<div id="divBox"> <?php $form = $this->beginWidget('CActiveForm') ?> <?php echo $form->textField($loginForm, 'username', array('id'=>'userName')) ?> <?php echo $form->passwordField($loginForm, 'password', array('id'=>'psd')) ?> <?php echo $form->textField($loginForm, 'captcha', array('id'=>'verify')) ?> <input type="submit" id="sub" value="login"/> <!-- 验证码 --> <div class="captcha"> <?php $this->widget('CCaptcha',array('showRefreshButton'=>false, 'clickableImage'=>true, 'imageOptions'=>array('alt'=>'点击换图', 'title'=>'点击换图', 'style'=>'cursor:pointer'))); ?> </div> <?php $this->endWidget() ?> <div class="four_bj"> <p class="f_lt"></p> <p class="f_rt"></p> <p class="f_lb"></p> <p class="f_rb"></p> </div> //验证,提示信息部分 <ul id="peo"> <li class="error"><?php echo $form->error($loginForm,'username') ?></li> </ul> <ul id="psd"> <li class="error"><?php echo $form->error($loginForm,'password') ?></li> </ul> <ul id="ver"> <li class="error"><?php echo $form->error($loginForm,'captcha') ?></li> </ul> </div>
这块我们首先先不讲代码,先讲一下 页面布局的问题,大家可能都很好奇我的这个页面 怎么只有 一个div,这就要谈到这个框架自带的定义页面公共部分的人性化功能,只需要在components/Controller.php中定义一下,公共部分的页面为//layouts/column1,定义代码如下:
class Controller extends CController { /** * @var string the default layout for * the controller view. * Defaults to '//layouts/column1', * meaning using a single column layout. * See 'protected/views/layouts/column1.php'. */ public $layout='//layouts/column1';
后端的公共页面在自己创建的那个模块里面定义,原理一样;
LoginController.php代码如下
<?php /** * 后台登陆控制器 */ class LoginController extends Controller{ /** * 后台登陆模板 */ public function actionIndex(){ $loginForm = new LoginForm(); if(isset($_POST['LoginForm'])){ $loginForm->attributes = $_POST['LoginForm']; if($loginForm->validate()&& $loginForm->login()){ Yii::app()->session['logintime'] = time(); $this->redirect(array('default/index')); } } $this->render('index',array('loginForm'=>$loginForm)); } public function actions(){ return array( 'captcha' => array( 'class' => 'system.web.widgets.captcha.CCaptchaAction', 'height' => 25, 'width' => 80, 'minLength'=> 4, 'maxLength'=> 4 ), ); } public function actionOut(){ Yii::app()->user->logout(); $this->redirect(array('index')); } }
这里面的LoginForm是框架自动生成的,我们需要做的就是,调整为我们自己需要的样子;
LoginForm.php代码 如下:
<?php /** * LoginForm class. * LoginForm is the data structure for keeping * user login form data. It is used by the 'login' action of 'SiteController'. */ class LoginForm extends CFormModel { public $username; public $password; public $rememberMe; public $captcha; private $_identity; /** * Declares the validation rules. * The rules state that username and password are required, * and password needs to be authenticated. */ public function rules() { return array( array('username', 'required', 'message'=>'用户名必须填写'), array('password', 'required', 'message'=>'密码必须填写'), // rememberMe needs to be a boolean array('rememberMe', 'boolean'), // password needs to be authenticated array('password', 'authenticate'), array('captcha', 'captcha', 'message'=>'验证码错误') ); } /** * Declares attribute labels. */ public function attributeLabels() { return array( 'username'=>'用户名', 'password'=>'密码', 'rememberMe'=>'Remember me next time', ); } /** * Authenticates the password. * This is the 'authenticate' validator as declared in rules(). * @param string $attribute the name of the attribute to be validated. * @param array $params additional parameters passed with rule when being executed. */ public function authenticate($attribute,$params) { if(!$this->hasErrors()) { $this->_identity=new UserIdentity($this->username,$this->password); if(!$this->_identity->authenticate()) $this->addError('password','用户性名或者密码错误'); } } /** * Logs in the user using the given username and password in the model. * @return boolean whether login is successful */ 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; } }
首先加上我们页面的验证码 的元素,框架自带的有这个js插件,我们就是机械性的使用一下就行了,然后就是设置我们的规则rules属性 ,这些规则 的具体意思可以看我的上一篇博客;
UserIdentity这个方法也是框架自带的(路径components/UserIdebtity),我们需要做的就是改成我们自己需要的样子;代码如下
public function authenticate() { $userInfo = User::model()->find('username=:name', array(':name'=>$this->username)); if($userInfo == NULL){ $this->errorCode=self::ERROR_USERNAME_INVALID; return false; } if($userInfo->password !== $this->password){ $this->errorCode=self::ERROR_PASSWORD_INVALID; return false; } $this->errorCode=self::ERROR_NONE; return true; }
这样我们的登陆就完成了,具体逻辑不再讲;
三、用户的增删改查
相关推荐
Yii Yii框架 Yii框架快速入门,自己整理的很全的,很容易看懂
Yii提供了今日Web 2.0应用开发所需要的几乎一切功能。Yii是最有效率的PHP框架之一。Yii是创始人薛强的心血结晶,于2008年1月1日开始开发。
《Yii框架深度剖析》配套代码 本书不是简单地介绍如何使用 Yii 框架,而是站在框架设计的高度,从源代码级别深 度剖析。本书首先介绍 PHP 框架技术的概念及其应用领域,然后开始仿照 Yii 框架源代码 自定义框架,...
yii框架学习笔记,主要详解请求的处理流程,yii组件、事件和行为,国际化,日志,视图,以及控制台应用。 带注释的源代码下载:https://github.com/kaimixu/yii-book
Yii 框架简介 全面轻松入手Yii 从这里开始 简单 明了 言简意赅
YII框架的开发中文文档,帮助哪些正在学习YII框架的同学们,
YII框架简介。学习yii框架必须要掌握的基础知识。
PHP分享一个功能强大的yii框架的分类树扩展,网上看到的,供大家一起共同分享学习。
教程名称: 2016最全Yii框架 YII是目前比较火的框架,框架里面使用了最新的技术和设计,可以说学习了YII就学习了最新的php技术,本课程主要学习XSS、CSRF、SQL注入、文件上传漏洞等攻击方式,以及YII框架对它们的...
Yii框架详解
yii2.0框架中文版手册,包含pdf和chm格式。提供大家学习!
这是一个基于yii框架编写的社区程序,主题仿豆瓣,要学习php强大的yii框架的可以当案例来研究下。
它是由Yii框架开发的青年文摘官网代码,有利于Yii初学者研究使用
这是开源PHP开发框架Yii全方位教程,想要学习yii框架吗,这个笔记可以快速让你了解yii,撑握yii
教程名称:Yii2.0 打造京东商城平台 PHP电商实战 Yii2.0视频教程 教程内容: 包含了电商的所有...不错的学习YII框架的基础课程,电商系统的结构完整、框架资源太大,传百度网盘了,链接在附件中,有需要的同学自取。
本文实例讲述了Yii框架学习笔记之应用组件操作。分享给大家供大家参考,具体如下: 所有的组件都应声明在config/web.php //组件声明在该数组下 'components'=>array( //自定义组件1 - 函数形式 'customComponent1...
主要介绍了Yii框架学习笔记之session与cookie简单操作,结合实例形式分析了Yii框架session与cookie的定义、设置、读写、删除等简单操作技巧,需要的朋友可以参考下
权威的yii框架中文版开发文档,希望能够为您的yii框架学习带来帮助,让你的技术更上一层楼! zhihuizhan.com
yii学习、PHP学习框架学习