Yii框架的安装

一.安装
1.查看php命令是否可用
php -v
如果有信息证明PHP安装好了,而且可用

2.在命令行下进行framework目录中

php yiic.php webapp ../cms


二.使用
1.修改默认控制器
在应用名/protected/config/main.php中的数组中加入
'defaultController'=>'Index', # 默认控制器

2.取得应用程序的相对URL 如 /blog 格式
Yii::app()->request->baseUrl
例:<link rel="stylesheet" href="<?php echo Yii::app()->request->baseUrl; ?>/assets/web/css/index.css">

3.分配数据
控制器
function actionIndex() {
$data = array(
'title'=>'文章网'
);
$this->renderPartial('index',$data);
}
视图
<?php echo $title; ?>

4.扩展自定义函数
在protected目录建立functions.php文件
在单入口引入函数
include './protected/functions.php';

5.布局

6. gii模块使用
1).打开模块,在config/main.php
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>'Enter Your Password Here',//设置密码
'ipFilters'=>array('127.0.0.1','::1')
);
把这个注释给去掉

如何来访问
http://yii.com/blog/index.php?r=admin/default/add
http://yii.com/blog/index.php?r=模块/控制器/方法

//自定义后台默认控制器
$this -> defaultController = "index";

7.小物件 widget
CActiveForm类下面找方法
<?php $form = $this->beginWidget('CActiveForm')?>
<?php echo $form->textField(模型,'表单名',html属性)?>
<?php $this->endWidget()?>

8.验证码使用
1).建立方法actions;
function actions(){
return array(
'captcha'=>array(
'class'=>'CCaptchaAction',
'height'=>25,
'width'=>80,
'minLength'=>4,
'maxLength'=>4
)
);
}或
这个写法是路径别名写法,其中system代表着 framework这个目录
function actions(){
return array(
'captcha'=>array(
'class'=>'system.web.widgets.captcha.CCaptchaAction',
'height'=>25,
'width'=>80,
'minLength'=>4,
'maxLength'=>4
)
);
}
后可以访问:http://yii.com/blog/index.php?r=admin/login/captcha
2).在视频中应用小物件
a.普通写法:
<?php $this->widget('CCaptcha')?>
b.带看不清楚,刷新功能
<?php
$this->widget('CCaptcha',array(
'showRefreshButton'=>false,
'clickableImage'=>true,
'imageOptions'=>array('alt'=>'点击换图','title'=>'点击换图','style'=>'cursor:pointer')
));
?>

在视图可以获得错误
<?php echo $form->error(模型名<$loginForm>,'captcha');?>

3).修改核心类CCaptchaAction
在此文件中找到run方法修改
$this->renderImage($this->getVerifyCode());
将其修改为:
$this->renderImage($this->getVerifyCode(true));即可,其实就在参了一个true过去就可以了

4).设置比对规则
在LoginForm.php文件中
public function rules()
{
return array(
// username and password are required
array('username, password', 'required'),
// rememberMe needs to be a boolean
array('rememberMe', 'boolean'),
// password needs to be authenticated
array('password', 'authenticate'),

array('captcha','captcha','message'=>'验证码错误'), # 这行就是比较验证码的
);
}

然后在:LoginController.php中加入验证即可
function actionIndex() {
$loginForm = new LoginForm();
if (isset($_POST['LoginForm'])) {
$loginForm->attributes = $_POST['LoginForm'];
if ($loginForm->validate()) {
echo 'code.....';
}
}
$this->render('index', array('loginForm' => $loginForm));
}

9.为后台(admin)模块设置单独布局文件
1).在protected里面的components文件夹复制一份,放到modules里面的模块目录(admin)下面,不需要components里面的UserIndenity.php
2).在模块下面的视图views目录里面建立layouts目录,里面放置模块的布局文件(例:adminlayout.php).
3).设置 public $layout='//layouts/xxx'; 设置为自己刚刚定义的布局文件(例:public $layout='/layouts/adminlayout'不需要扩展名),还有去掉1个前面/,只保留一个即可
4).如果后台模块没有公共区域直接在布局文件里面写<?php echo $content;?>

10.数据库连接配置
1).在main.php里面开启
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=testdrive',
'emulatePrepare' => true, //PDO扩展
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix'=>'yii_',// 定义表前缀
'enableParamLogging'=>true //开启试息SQL语句具体值
),

11.开启SQL调试信息界面

12.定义模型Model
在项目/proected/models/最好以数据表名一致来使名<User.php>
/**
* 后台用户模型
*/
class User extends CActiveRecord{

/**
* 必不可缺方法1,返回模型
* @param type $className
* @return type
*/
static function model($className = __CLASS__) {
return parent::model($className);
}

/**
* 必不可缺方法2,返回表名 不用写前缀,用两个花括号括起来
* @return type
*/
public function tableName() {
return "{{admin}}";
}
}


13.登陆验证
1).LoginForm.php
public function rules() {
return array(
array('username','required','message'=>'用户名不能为空'),
array('password','required','message'=>'密码不能为空'),
array('password', 'authenticate'),
array('captcha', 'captcha', 'message' => '验证码错误'),
);
}
2).UserIdentity.php
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 != md5($this->password)) {
$this->errorCode = self::ERROR_PASSWORD_INVALID;
return false;
}
$this->errorCode = self::ERROR_NONE;
return true;
}
3).LoginController.php
function actionIndex() {
#$userInfo = User::model()->find('username=:name', array(':name'=>'admin'));
#p($userInfo);
$loginForm = new LoginForm();
if (isset($_POST['LoginForm'])) {
$loginForm->attributes = $_POST['LoginForm'];
if ($loginForm->validate() && $loginForm->login()) {
echo 11111;
}
}
$this->render('index', array('loginForm' => $loginForm));
}

#######################################################

1.让表单在前台也验证,小物件写法:

<?php
$form = $this->beginWidget('CActiveForm',array(
'htmlOptions'=>array('enctype'=>'multipart/form-data'),
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true
)
));

............

<table class="table">
<caption>发表文章</caption>
<tr>
<td class="right"><?php echo $form->labelEx($articleModel,'title'); ?>:</td>
<td>
<?php echo $form->textField($articleModel,'title',array('maxlength'=>20)); ?>
<?php echo $form->error($articleModel,'title') ?>
</td>
</tr>
<tr>
<td class="right"><?php echo $form->labelEx($articleModel,'type'); ?>:</td>
<td>
<?php
echo $form->radioButtonList(
$articleModel,
'type',
array(0=>'普通',1=>'热门'),
array('separator'=>'&nbsp;')
);
echo $form->error($articleModel,'type');
?>
</td>
</tr>
<tr>
<td class="right"><?php echo $form->labelEx($articleModel,'cid'); ?>:</td>
<td>
<?php
echo $form->dropDownList(
$articleModel,
'cid',
$cateInfoArr
);
echo $form->error($articleModel,'cid');
?>
</td>
</tr>
<tr>
<td class="right"><?php echo $form->labelEx($articleModel,'pic'); ?>:</td>
<td>
<?php
echo $form->fileField($articleModel,'pic');
echo $form->error($articleModel,'pic');
?>
</td>
</tr>
<tr>
<td class="right"><?php echo $form->labelEx($articleModel,'info'); ?>:</td>
<td>
<?php
echo $form->textArea($articleModel,'info',array('cols'=>50,'rows'=>10,'maxlength'=>100));
echo $form->error($articleModel,'info');
?>
</td>
</tr>
<tr>
<td class="right"><?php echo $form->labelEx($articleModel,'content'); ?>:</td>
<td>
<?php
echo $form->textArea($articleModel,'content',array('id'=>'content'));
echo $form->error($articleModel,'content');
?>
</td>
</tr>
<tr>
<th colspan="2">
<input type="submit" value="发表文章">
</th>
</tr>
</table>

............

$this->endWidget();
?>

2.上传图片:

在控制器表单入库程序中写:

//上传图片
$articleModel->pic = CUploadedFile::getInstance($articleModel, 'pic');
if ($articleModel->pic) {
$pre_rand = 'img_' . date('YmdHis') . mt_rand(0, 9999);
$imgName = $pre_rand . '.' . $articleModel->pic->extensionName;
$articleModel->pic->saveAs('uploads/' . $imgName);
$articleModel->pic = $imgName;

//缩略图 这里的缩略图是自己来扩展的,位置可放在 项目名/protected/extensions/自定义名/自定义文件名[这里的文件名和类名要统一]

后在main.php中找到components这里,插入以下内容 ext是别名扩展即代表为extensions的目录,thumb这个即Yii::app()调用的名称 class指定类的位置

注:这里的类要继承:CApplicationComponent类:

如:

/**
* 图像处理类
*/
class Image extends CApplicationComponent {

}

'components' => array(
     'thumb'=>array(
        'class'=>'ext.CThumb.Image'
      ),

);


/*

$path = dirname(Yii::app()->basePath) . '/uploads/';
$image = Yii::app()->thumb;
$outFile = $image->thumb($path . $imgName, '', 130, 95);

*/
}

3.自带分页的写法:

控制器:

$cri = new CDbCriteria();
$artModel = Article::model();
$total = $artModel->count($cri);

$pager = new CPagination($total);
$pager->pageSize = 2;
$pager->applyLimit($cri);

$artInfo = $artModel->findAll($cri);

$data = array(
'artInfo' => $artInfo,
'pages' => $pager
);

$this->render('index',$data);

视图:

<?php
$this->widget(
'CLinkPager',
array(
'header'=>'',
'firstPageLabel'=>'首页',
'lastPageLabel'=>'末页',
'prevPageLabel'=>'上一页',
'nextPageLabel'=>'下一页',
'pages'=>$pages,
'maxButtonCount'=>5
)
);
?>

以上是自带的分页写法,当然也可以用自己写的分页类,这样就更加的灵活和好控制具体的方法还是扩展,这里用另一个扩展方案:

不用继承CApplicationComponent类,直接将自己写好的类放入 项目/protected/components 目录中即可,这个目录,在项目运行时,里面的所用文件将会自动被加载进来.

在分页的地方只需要:

$page = new Page();即可.

4.关联模型的写法:

在model模型中写入这里的self::后面的类型有多种,根据情况来定

function relations() {
return array(
'cate'=>array(self::BELONGS_TO,'Cate','cid')
);
}

5.权限认证:

/**
* 文章管理控制器
*/
class ArticleController extends Controller {

/**
* * 代表所有用户
* @ 代表登陆用户
* ? 代表匿名用户
*/
function filters() {
return array(
// 'accessControl - index' 这样写代表 文章控制器中的actionIndex方法不需要验证 也可以用+加,代表需要验证
'accessControl',
);
}
function accessRules() {
return array(
array(
'allow',
'actions' => array('index', 'add', 'del'), # 方法名 可以访问的方法
'users' => array('@'), # @代表已经验证过的用户 登陆过后的用户 也可以具体化可以写admin用户
),
array(
'deny',
'users' => array('*'), # * 全部用户
)
);
}

}

############## 路由和缓存

1.隐藏单入口文件

保证apache配置文件httpd.conf里面LoadModule rewrite_module开启

将相对应目录的AllowOverride 改为 All

在根目录下,即index.php同级目录下新建.htaccess文件

.htaccess内容

RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php

重启apache

2.路由

在proected/config/main.php

'urlManager'=>array(
   'urlFormat'=>'path', //改为path格式路径
   'showScriptName'=>false, //去掉index.php
   'rules'=>array(
       'index.html'=>array('index'),  // # 前面是用户访问规则=>访问的控制器

       'a/<aid:d+>'=>array('article/index','urlSuffix'=>'.html'), //规则设置,后缀为.html

       # 最一条的访问url为   http://www.xxx.com/a/12.html 对应  http://www.xxx.com/article/index/aid/12

   ),

),

3.缓存

开启缓存

在main.php的组件components中配置设置缓存

'cache'=>array(

'class'=>'system.caching.CFileCache' # 这里有多个缓存机制,可以根据需求来自定义,这里我选择了文件缓存

)

a.片段缓存

<?php if($this->beginCache($id,array('duration'=>1))){>

缓存内容

<?php $this->endCache();}?>

duration时间单位为秒

b.整页缓存

public function filters(){

  return array(

  array(

  'system.web.widgets.COutputCache + index',

     'duration'=>30,

     'varByPara'=>array('aid')

  )

  );

}

c.数据缓存

$value = Yii::app()->cache->get($id);

if($value==false){

$value = Yii::app()->cache->set($id,$value,10); # 最后为缓存时间,单位为秒

}

d.动态缓存

原文地址:https://www.cnblogs.com/ahwu/p/3477056.html