1.目录结构
assets 一般存放CSS,IMGSE图片文件
2.修改默认控制器方法
参数r 是router路由意思的缩写
main.php 'defaultController' => 'Index', //设置默认控制器为Index
render渲染模板方法
$this->render('index'); //render 默认会载入layouts默认的布局文件
$this->renderPartial('index'); //不载入默认布局文件 也不载入JQUERY文件
3.布局文件
修改布局文件
(1).view/layouts下面新建好布局文件
(2).修改默认布局文件:
位置:components/Controller.php public $layout='//layouts/column1'; 修改后面为//layouts/blog
blog.php文件格式
!---
公共文件头
<?php echo $content ?> 这里是不公共的部分写法
公共文件尾
!---
4.载入外部文件
Yii::app()->request->baseUrl; 项目根目录 例如:<?php echo Yii::app()->request->baseUrl;?>/assets/Home/Style/topic_saishi.css
5.视图分配数据
$data=array(
'title'=>'yii学习'
)
$this->render('index',$data);
模板文件直接<?php echo $title ?>
6.自定义函数
(1)protected目录下面新建function.php 然后在里面写函数格式
(2)在入口文件中载入自定义函数文件
7.gii自动化的使用
'modules' => array(
// uncomment the following to enable the Gii tool
'gii' => array(
'class' => 'system.gii.GiiModule', //system表示框架路径
'password' => 'yii',
// If removed, Gii defaults to localhost only. Edit carefully to taste.
'ipFilters' => array('127.0.0.1', '::1'),
),
),
模块说明
Controller Generator 创建控制器
Crud Generator 创建数据库
Form Generator 创建表单
Model Generator 创建模型
Module Generator 创建模块
8后台模块建立
(1).Module Generator 创建模块以后 会生成 protected/modules
(2).配置文件main.php modules里面添加模块 ‘admin’
(3).小物件表单创建 widget使用
使用方法:
CActiveForm类下面找方法
<?php $form=$this->beginWidget(' CActiveForm ') ?>
<?php echo $form->textField(模型,'表单控件名',html属性) ?>
注意这里面的表单控件名,必须对应模型类里面的public 变量属性
<?php $this->endWidget() ?>
在控制器中实例化模型并分配到模板上
<?php
class LoginController extends Controller {
public function actionIndex() {
$loginFrom=new LoginForm();
$this->renderPartial('index',array('loginFrom'=>$loginFrom));
}
}
(4)验证码:
1.public function actions() {
return array(
'captcha' => array(
'class' => 'system.web.widgets.captcha.CCaptchaAction',
'height' => 25,
'width' => 80,
'minLength' => 4,
'maxLength' => 4,
)
);
}
2. 模板使用验证码
a)<?php $this->widget('CCaptcha'); ?>
b)<?php $this->widget('CCaptcha',array('showRefreshButton'=>false,'clickableImage'=>true,'imageOptions'=>array('alt'=>'点击换图','title'=>'点击换图','style'=>'cursor:pointer'))); ?>
3.修改验证码核心类
run 方法的
$this->renderImage($this->getVerifyCode(true));
4.模型验证规则(loginFrom.php)
public function rules(){
array('captcha', 'captcha', 'message' => '验证码错误'),
}
5.控制器触发验证
$loginFrom = new LoginForm(); //除了添加是new 其他都是直接使用
if (isset($_POST['LoginForm'])) {
$loginFrom->attributes = $_POST['LoginForm']; 压入模型
if($loginFrom->validate()&&$loginFrom->login()){ 验证是否通过&&是否登录成功
}
}
//模板里面错误提示 <?php echo $form->error($loginFrom,' captcha ') ?>
列如:
<form action="#" method="post" name="form1" id="form1">
//被替换为 <?php $form=$this->beginWidget(' CActiveForm ') ?>
用户名
<input name="username" type="text" class="ie6" id="username">
//被替换为<?php echo $form->textField($loginFrom,' username ',array('id'=>'username')) ?>
密码
//被替换为<?php echo $form->passwordField($loginFrom,' password',array('id'=>'username')) ?>
验证码
<input name="verify" type="text" class="ie6" id="verify"/>
<input type="submit" name="submit" id="button" value="登陆" class="button orange"/>
</form>
//<?php $this->endWidget() ?>
(5)修改后台的布局文件
view 建立layouts/admin.php文件 admin/components/Controller 修改为public $layout='
/layouts/admin';
如果没有公共文件 直接<?php echo $content; ?>即可
------用户登录-------
(6)user模型的建立 (必须包含2个方法 固定写法)
数据库的连接
'db' => array(
'connectionString' => 'mysql:host=localhost;dbname=yii',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => 'hd_',
'enableParamLogging' => true, //开启调试信息
),
定义模型 (前后公用模型)
<?php
class User extends CActiveRecord{
public static function model($className = __CLASS__){
return parent::model($className);
}
public function tableName(){
return "{{user}}"; //对应的表名
}
登录使用登录模型LoginFrome
所以在登录模型LoginFrome里面定义验证规则
然后模板上调取错误信息 <?php echo $form->error($loginFrom,'username') ?>
//自定义验证的方法
查询数据(UserIdentity插件)
public function authenticate() {
$userInfo=User::model()->find('username=:name',array(':name'=> $this->username));
if($userInfo==NULL){
return false;
}
if($userInfo->password!==md5($this->password)){
$this->errorCode = self::ERROR_PASSWORD_INVALID;
return false;
}
$this->errorCode = self::ERROR_NONE;
return true;
}
Yii::app()->user->name调用session信息
为了区分前后台session信息
在后台AdminModule.php的init方法里面添加
标签定义(定义在模型里面)
user.php 模型
public function attributeLabels(){
return array(
'password'=>'原始密码',
'password1'=>'新密码', //这个需要定义 public $password1 因为数据库没有这个字段
'password2'=>'确认新密码', //这个需要定义 public $password2 因为数据库没有这个字段
);
}
在模板上使用 <?php echo $form->labelEx($usermodel[定义的模型],'password'[定义的名称]) ?> labelEx表示必填选项 label表示非必填
修改提示密码 并设置提示信息
if($userModel->updateByPk($userinfo->uid,array('password'=>$password))){
Yii::app()->user->setFlash('success','修改密码成功'); //设置提示信息
}
模板读取提示信息
if(Yii::app()->user->hasFlash('success')
){
echo Yii::app()->user->getFlash('success');
}
开启调试模式
main.php
log
array(
'class'=>'CWebLogRoute',
),
AR类实现增删改查