php生成全静态页面的方法

1.在数据库表config表中字段key,val,name的值中加入

2.在生成文章的控制器里面:

public function actionAdd() {
//保存之后
// 生成html
$this->actionPublish();
}

public function actionPublish(){
        $cid = $_POST['cid'];
        $content = Content::model()->findByPk($cid);
        $article = CArticle::model()->findByAttributes(array('content_id' => $cid));      //文章内容表(两个字段),一个是content_id,一个是body内容
        $file = ($content->html_url != '')? $content->html_url : Content::model()->getHtmlMakePath($cid, $content['createtime']);
        $config = Config::model()->getConfigs();
        $set = array(
            'cid' => $cid,
            'title' => $content['title'],
            'from' => '',
            'published' => $content['createtime'],
            'content' => $article['body'],
            'config' => $config
        );
        Yii::import("application.helpers.template.CTemplate");     //静态化的类
        $template = new CTemplate();
        $template->set($set);
        $template->setHtmlPath($file);
        $template->makeHtml('default/article.html');
        if ($content->html_url == '') {
            $content->html_url = $file;
            $content->save();
        }
    }

3.文章类content.php

    public function getHtmlMakePath($id, $time){
        $config = Config::model()->getConfigs();
        $path = '/html/' . date("Y/m/d/", $time) . 'detail_' . $id . '.html';
        // 获取系统配置静态页路径生成规则
        if (isset($config['html_detail_path']) && $config['html_detail_path']) {
            $year = date("Y", $time);
            $month = date("m", $time);
            $day = date("d", $time);
            $search = array('{y}', '{m}', '{d}', '{id}');
            $replace = array($year, $month, $day, $id);
            $path = str_replace($search, $replace, $config['html_detail_path']);
        }
        return $path;
    }

配置类config.php

    public function getConfigs(){
        $cache_name = md5('model_Config_getConfigs');
        $configs = Yii::app()->memcache->get($cache_name);
        if (!$configs) {
            $data = $this->findAll();
            $configs=array();
            foreach ($data as $one) {
                $configs[$one->key] =$one->val;
            }
            Yii::app()->memcache->set($cache_name, $configs, 300);
        }
        return $configs;
    }

4.主要要引进静态化的类

 

原文地址:https://www.cnblogs.com/fengzhiqiangcaisangzi/p/3382114.html