PHPCMS V9详解如何生成静态化HTML及配置URL规则

jerry CMS 2015年08月30日 收藏

先讲讲Phpcms V9在后台怎么设置生成静态化HTML,之后再讲解怎么自定义URL规则,进行URL地址优化。在这一篇中,伪静态就不涉及了,大家可以移步到Phpcms V9全站伪静态设置方法。


phpcms生成的静态页目录规则是按照URL规则生成的, 静态页 内容是ob_clean前台模板文件产生的phpcms\templates\default,静态页的调试 :修改静态页,重新生成静态页再看效果,也可以调用前台控制/index.php?m=openservice&c=index&a=openservice修改好前台模板再生成。只要是静态页的链接都要及时同步文件,防止死链接,数据错误

  1. $urlrule = $site_info['domain'].'/webgame/index.html~'.$site_info['domain'].'/webgame/page_{$page}.html';   
  2. define('URLRULE', $site_info['domain'].'/webgame/{$webgamedir}/index.html~'.$site_info['domain'].'/webgame/{$webgamedir}/spec_{$page}.html');             
  3. $GLOBALS['URL_ARRAY'] = array('webgamedir'=>$filename);

后台生成静态页的类phpcms\modules\*\classes\html.class.php,在后台控制器中调用

  1. /** 
  2.  * 生成开服首页 
  3.  */  
  4. public function create_index() {  
  5.     $html = pc_base::load_app_class('html');  
  6.     $size = $html->create_list();  
  7.     showmessage(L('index_create_finish',array('size'=>sizecount($size))));     
  8. }  
  9. $html = pc_base::load_app_class('html', 'content'); 调用content下的html.class.php

array_shift递归调用批量生成静态页

  1. /** 
  2.      * 生成静态文件 
  3.      * @param string $file 文件路径 
  4.      * @return boolen/intval 成功返回生成文件的大小 
  5.      */  
  6.     private function create_html($file) {  
  7.         $data = ob_get_contents();  
  8.         ob_end_clean();  
  9.         pc_base::load_sys_func('dir');  
  10.         dir_create(dirname($file));  
  11.         $strlen = file_put_contents($file, $data);  
  12.         @chmod($file, 0777);  
  13.         return $strlen;  
  14.     }  
  15.           
  16.     /** 
  17.      * 生成列表页 
  18.      */  
  19.     public function create_list() {  
  20.         $siteid = get_siteid();  
  21.         //分站时计算路径  
  22.         if ($siteid>1) {  
  23.             $site_info = $this->site->get_by_id($siteid);  
  24.             $file = pc_base::load_config('system', 'html_root').'/'.$site_info['dirname'].'/openservice/index.html';  
  25.         } else {  
  26.             $file = pc_base::load_config('system', 'html_root').'/openservice/index.html';  
  27.         }  
  28.         $this->queue->add_queue('add', $file, $siteid);  
  29.         $file  = PHPCMS_PATH.$file;  
  30.         ob_start();  
  31.         include template('openservice', 'index');  
  32.         return $this->create_html($file);  
  33.     }     
  34. //生成专题首页控制中心  
  35.     public function public_create_html() {  
  36.         $specials = getcache('create_specials', 'commons');  
  37.         if (is_array($specials) && !empty($specials)) {  
  38.             $specialid = array_shift($specials);  
  39.             setcache('create_specials', $specials, 'commons');  
  40.             $this->create_index($specialid);  
  41.         } else {  
  42.             delcache('create_specials', 'commons');  
  43.             showmessage(L('update_special_success'), '?m=special&c=special&a=init');  
  44.         }  
  45.     }  
  46.       
  47.     //生成某专题首页  
  48.     private function create_index($specialid) {  
  49.         $info = $this->db->get_one(array('id'=>$specialid));  
  50.         $html = pc_base::load_app_class('html');  
  51.         $html->_index($specialid);  
  52.         showmessage($info['title'].L('update_success'), '?m=special&c=special&a=public_create_html');  
  53.     }


案例一:


一、静态化HTML生成设置

进入PHPCMS V9后台设置,找到分类栏目,位置:内容->管理栏目->添加栏目(或者管理栏目、点击对应栏目“修改”):
201310221901442[1].jpg
注意设置第二选项卡,“生成HTML设置”,具体抓图:
201310221901443[1].jpg
确认提交后,记得更新栏目缓存,然后进行发布管理:批量更新栏目页、批量更新内容页。

二、PHPCMS V9自定义URL规则及URL地址优化

先了解下PC v9的html生成原理:

Phpcms生成的静态页目录规则是按照URL规则生成的, 静态页内容是ob_clean前台模板文件产生的phpcms\templates\default,静态页的调试 :修改静态页,重新生成静态页再看效果,也可以调用前台控制/index.php?m=openservice&c=index& a=openservice修改好前台模板再生成。

只要是静态页的链接都要及时同步文件,防止死链接,数据错误。

基于此,我们可以在后台扩展URL规则管理中自定义URL生成规则,具体位置:扩展 > URL规则管理 。

下一页,将具体分享CMSYOU对优化URL生成规则的认识和实例。

1、默认的栏目生成规则是:

  1. {$categorydir}{$catdir}/index.html|{$categorydir}{$catdir}/{$page}.html

具体生成html的时候,将会显示成:news/cmsyou/1000.html。

这个有点小问题,如果列表有多页,那么第二页只是生成一个数字的html,比如2.html表示列表第二页,CMSYOU建议修改成:

  1. {$categorydir}{$catdir}/index.html|{$categorydir}{$catdir}/index_{$page}.html

按照栏目index页面生成,栏目列表变成:index.html、index_2.html、index_3.html……这样比较明朗。

如果要自定义生成到某一个固定的目录,可以在前面添加一个特定的英文目录。

2、文章页面的默认生成规则是:

  1. {$year}/{$catdir}_{$month}{$day}/{$id}.html|{$year}/{$catdir}_{$month}{$day}/{$id}_{$page}.html

或者:

  1. {$categorydir}{$catdir}/{$year}/{$month}{$day}/{$id}.html|{$categorydir}{$catdir}/{$year}/{$month}{$day}/{$id}_{$page}.html

这样的规则生成的HTML网页目录太深了,CMSYOU建议修改为:

  1. {$categorydir}{$catdir}/{$id}.html|{$categorydir}{$catdir}/{$id}_{$page}.html

这样生成的HTML静态文件,仅仅是在栏目的根部,这样便于管理,也利于SEO。比如xxx/support/84.html这个,就是在CMSYOU帮助文章下面的一篇文章,去掉后面的84.html就是栏目首页,这样直接。

3、单页面生成规则优化

默认的单页面生成方式只有一种,CMSYOU建议增加一种:

  1. {$categorydir}{$catdir}.html

于是二级目录的单页面只生成一个html,而不生成一个目录,这样有利于HTML架构。分享设置抓图:

201310221901444[1].jpg
这样,具体分享完Phpcms V9的栏目、内页、单页面的生成规则及优化实例,你是否了解了自定义HTML生成规则?


案例二:


到扩展 > URL规则管理 > 增加规则

首先添加一个栏目/列表页url生成规则
20140702095646768[1].jpg
然后,再添加一个内容页url生成规则
20140702095658194[1].jpg
最后,在栏目管理里面进行html生成管理
20140702095710919[1].jpg

  这样就可以大功告成了。