Yii 1.1.17 六、开启路由与使用缓存

一、开启路由

1、在配置文件main.php的components中 定义如下:

// 定义路由
'urlManager'=>array(

    // URL模式为PATHINFO
    'urlFormat'=>'path',

    //隐藏index.php入口名
    'showScriptName' => false,

    //路由规则
    'rules'=>array(

        //要映射的前台地址 => 实际地址 的路由映射
        'index.html' => array('index/index'), //映射到 前台首页(记得更改"首页"链接的url为index.html,保持一致)
        'c_<id:d+>' => array('category/index', 'urlSuffix' => '.html'),
        '<id:d+>' => array('article/index', 'urlSuffix' => '.html')

    ),
),

  

2、配置Apache开启路由

资料很多,就不写了。

二、使用缓存

1、配置

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

// 开启缓存
'cache' => array(
    // 定义缓存方式为 文件缓存
    'class' => 'system.caching.CFileCache'
),

    

2、使用

片段缓存

在视图中,

<?php if($this->beginCache('nav', array('duration' => 3600 * 24))): /* 片段缓存,“秒” 为单位,缓存1天 */?>
     // HTML内容
<?php $this->endCache(); endif; ?>

  

整页缓存

在控制器的filters()方法中,

/**
* 过滤器:对index操作进行整页缓存
* @return [type] [description]
*/
public function filters(){
return array(
    array(
        'system.web.widgets.COutputCache + index', //对index操作设置整页缓存
        'duration' => 3600, //缓存1小时
        'varyByParam' => array('id') //通过id进行缓存

        )
    );
}

    

数据缓存

在控制器的方法中,

if(!$value = Yii::app()->cache->get($id)){ //如果有缓存则读取,否则生成缓存
    //缓存1小时
    Yii::app()->cache->set('id', $value, 3600);
}

  

OK,项目完成后记得关闭 gii 模块和调试信息

Yii 1.1.17入门笔记至此结束

原文地址:https://www.cnblogs.com/mingc/p/6752306.html