网站之家技术交流论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1318|回复: 0

PHPCMS V9自定义栏目伪静态实现方法(列表页/分页/内容页)

[复制链接]
发表于 2016-1-10 12:43:36 | 显示全部楼层 |阅读模式

phpcmsV9 9.5.8 栏目伪静态的修改方法(支持自定义目录名),官方程序默认伪静态是不支持自定义栏目名的,所以今天就做了以下修改,让其支持!

  首先看urlrewrite的规则,这个是Apache下的,其它环境下的规则自己转换下

  1. RewriteEngine on

  2. #静态文件以及API目录不需要伪静态
  3. RewriteRule ^(statics|api|uploadfile)(.*) - [L]

  4. #内容页
  5. RewriteRule ^([0-9A-Za-z_/]*)/([0-9]+)\.html index.php?m=content&c=index&a=show&dir=$1&id=$2

  6. RewriteRule ^([0-9A-Za-z_/]*)/([0-9]+)_([0-9]+)\.html index.php?m=content&c=index&a=show&dir=$1&id=$2&page=$3

  7. #栏目页
  8. RewriteRule ^([0-9A-Za-z_/]*)/index_([1-9][0-9]*)\.html index.php?m=content&c=index&a=lists&dir=$1&page=$2

  9. RewriteRule ^([0-9A-Za-z_/]*)/ index.php?m=content&c=index&a=lists&dir=$1
复制代码

URL规则栏目页:{$categorydir}{$catdir}/|{$categorydir}{$catdir}/index_{$page}.html

URL规则内容页: {$categorydir}{$catdir}/{$id}.html|{$categorydir}{$catdir}/{$id}_{$page}.html


 1、打开phpcms\modules\content目录下的index.php找到 public function lists() {,将$catid = intval($_GET['catid']);替换成:
  1. if(isset ($_GET['catid'])){   
  2.                     $catid = intval($_GET['catid']);   
  3.                 }else{   
  4.                     $catdir=$_GET['catdir'];   
  5.                     if($catdir==""){   
  6.                         $catdir=$_GET['categorydir'];   
  7.                     }   
  8.                     $s=$this->_getCategoryId($catdir);   
  9.                     $catid=$s[0][catid];                     
  10.                 }
复制代码
并且在最后的}?> 前添加:
  1. /**           *根据栏目名获得ID           * @param <type> $catdir           */  
  2.         function _getCategoryId($catdir){   
  3.             $this->category_db = pc_base::load_model('category_model');   
  4.             $result = $this->category_db->select(array('catdir'=>$catdir));   
  5.            // print_r($result);   
  6.             return $result;   
  7.          }
复制代码
先增加一个方法,用于将`catdir`转换为`catid`
这是参考站长之家的文章修改而来的,其中获取查询category,我其实一直在纠结是要直接读取cache好还是查询数据库好,读取cache又需要遍历数组,栏目的数量一多的话效率恐怕也没有那么高,增加到 //首页  前面
  1. private function _getCategoryId($catdir){
  2.                 if(!strpos($catdir,'/')) {
  3.                         $dirname = $catdir;
  4.                 }else {
  5.                         $dirname = end(explode('/',$catdir));
  6.                 }
  7.                 $this->category_db = pc_base::load_model('category_model');
  8.                 $result = $this->category_db->get_one(array('catdir'=>$dirname));
  9.                 return $result['catid'];
  10.         }
复制代码
分别在`lists()`和`show()`方法中,将获取`catid`的语句修改
  1. $catid = intval($_GET['catid']
复制代码
替换
  1. //$catid = intval($_GET['catid']);
  2.                 if(isset ($_GET['catid'])){
  3.                         $catid = intval($_GET['catid']);
  4.                 }else{
  5.                         $catid=$this->_getCategoryId($_GET['dir']);
  6.                 }
复制代码

然后在phpcms后台,添加两条自定义的URL规则


然后再更新栏目缓存、批量更新URL就可以看到效果了


找到 //URL规则下面的

  1. $GLOBALS['URL_ARRAY']['categorydir'] = $categorydir;
  2. $GLOBALS['URL_ARRAY']['catdir'] = $catdir;
  3. $GLOBALS['URL_ARRAY']['catid'] = $catid;
复制代码
替换
  1. $GLOBALS['URL_ARRAY']['categorydir'] = $parentdir;
  2. $GLOBALS['URL_ARRAY']['catdir'] = $catdir;
  3. $GLOBALS['URL_ARRAY']['catid'] = $catid;
复制代码

2、打开phpcms\modules\content\classes目录中的url.class.php,找到

  if (!$setting['ishtml']) { //如果不生成静态

  将下面的:

  1. $url = str_replace(array('{$catid}', '{$page}'), array($catid, $page), $urlrule);   
  2.             if (strpos($urls, '\\')!==false) {  
  3.                     $url = APP_PATH.str_replace('\\', '/', $urls);   
  4.             }
复制代码
替换成:
  1. $domain_dir = '';   
  2.             if (strpos($category['url'], '://')!==false && strpos($category['url'], '?')===false) {   
  3.                 if (preg_match('/^((http|https):\/\/)?([^\/]+)/i', $category['url'], $matches)) {   
  4.                     $match_url = $matches[0];   
  5.                     $url = $match_url.'/';   
  6.                 }   
  7.                 $db = pc_base::load_model('category_model');   
  8.                 $r = $db->get_one(array('url'=>$url), '`catid`');   
  9.   
  10.                 if($r) $domain_dir = $this->get_categorydir($r['catid']).$this->categorys[$r['catid']]['catdir'].'/';   
  11.             }   
  12.             $categorydir = $this->get_categorydir($catid);   
  13.             $catdir = $category['catdir'];   
  14.             $year = date('Y',$time);   
  15.             $month = date('m',$time);   
  16.             $day = date('d',$time);   
  17.             //echo $catdir;   
  18.             $urls = str_replace(array('{$categorydir}','{$catdir}','{$year}','{$month}','{$day}','{$catid}','{$id}','{$prefix}','{$page}'),array($categorydir,$catdir,$year,$month,$day,$catid,$id,$prefix,$page),$urlrule);   
  19.                        // echo $urls."
  20. ";   
  21.                         if (strpos($urls, '\\')!==false) {  
  22.                     $urls = APP_PATH.str_replace('\\', '/', $urls);   
  23.             }   
  24.                         $url = $domain_dir.$urls;
复制代码

更新栏目缓存就OK了。


分页问题处理

  1. file:  \phpcms\libs\functions\global.func.php


  2. /**
  3. * 返回分页路径
  4. *
  5. * @param $urlrule 分页规则
  6. * @param $page 当前页
  7. * @param $array 需要传递的数组,用于增加额外的方法
  8. * @return 完整的URL路径
  9. */
复制代码
  1. function pageurl($urlrule, $page, $array = array()) {
  2.     if(strpos($urlrule, '~')) {
  3.         $urlrules = explode('~', $urlrule);
  4.         $urlrule = $page < 2 ? $urlrules[0] : $urlrules[1];
  5.     }
  6.     $findme = array('{$page}');
  7.     $replaceme = array($page);
  8.     if (is_array($array)) foreach ($array as $k=>$v) {
  9.         $findme[] = '{$'.$k.'}';
  10.         $replaceme[] = $v;
  11.          
  12.     }
  13.      

  14.     $url = str_replace($findme, $replaceme, $urlrule);
  15.     $url = str_replace(array('http://','//','~'), array('~','/','http://'), $url);
  16.     if (!strstr($url,siteurl(get_siteid()))){
  17.                 $url = siteurl(get_siteid()) . '/' . $url;
  18.         }
  19.     return $url;  
  20.      
  21. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|网站之家技术交流论坛 ( 粤ICP备09092995号 )

GMT+8, 2024-5-4 02:15 , Processed in 0.086851 second(s), 6 queries , File On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表