网站首页学无止境PHP

PHPCMS V9自定义栏目伪静态实现方法

发布时间:2014-07-23 16:35:17编辑:songlin阅读(273)

        最近用phpcms V9做个站,需要做栏目伪静态,上网查了好久发现都是几年以前发布的,由于phpcms更新的比较频繁,有好多规则都已经改变了。没办法只能自己在写一下,当然也有copy别人的东西(有现成的为毛不用)。本文的方法是用于最新发布的几个phpcms v9版本。本文的修改方法支持自定义目录名,例如www.xxx.com/news/。官方程序默认伪静态是不支持自定义栏目名的,所以今天就做了以下修改,让其支持!
     
      首先看urlrewrite的规则,这个是linux下Apache的.htaccess 文件部分代码,其它环境下的规则如有需要自己转换
     
     
    1. RewriteRule /phpcms/(.*)(.*)/ /phpcms/index\.php\?m=contentc=indexa=listscategorydir=$1catdir=$2 
    2.  
    3. RewriteRule /phpcms/(.*)(.*)/([0-9]+)/ /phpcms/index\.php\?m=contentc=indexa=listscategorydir=$1catdir=$2page=$3 

     

     
      1、打开phpcms\modules\content目录下的index.php找到 public function lists() {,将

        
    1. $catid = intval($_GET['catid']); 


       替换成:
     
      
    1. if(isset ($_GET['catid'])){ 
    2.  
    3.      $catid = intval($_GET['catid']); 
    4.  
    5.   }else
    6.  
    7.      $catdir=$_GET['catdir']; 
    8.  
    9.   if($catdir==""){ 
    10.  
    11.      $catdir=$_GET['categorydir']; 
    12.  
    13.   } 
    14.  
    15.   $s=$this->_getCategoryId($catdir); 
    16.  
    17.   $catid=$s[0][catid]; 
    18.  

     

     
      并且在最后的

        
    1.    }
    2. ?> 


       前添加:
     
      
    1. /**   
    2.     *根据栏目名获得ID   
    3.     * @param  $catdir   
    4.     */   
    5.     function _getCategoryId($catdirs){   
    6.         $this->category_db = pc_base::load_model('category_model'); 
    7.         $catdirarr=explode('/',trim($catdirs,'/')); 
    8.         $catdir=array_pop($catdirarr); 
    9.         $parentdirarray_pop($catdirarr); 
    10.         $parentdir = $parentdir!=null ? $parentdir.'/'''
    11.         $result = $this->category_db->get_one(array('parentdir'=>$parentdir,'catdir'=>$catdir));    
    12.         return $result;    
    13.     } 

     

     
      2、打开phpcms\modules\content\classes目录中的url.class.php,找到
     
      if (!$setting['ishtml']) { //如果不生成静态
     
      将下面的:
     
      
    1. $url = str_replace(array('{$catid}''{$page}'), array($catid$page), $urlrule); 
    2.  
    3.   if (strpos($urls'\\')!==false) { 
    4.  
    5.    $url = APP_PATH.str_replace('\\', '/', $urls); 
    6.  
    7.  } 

     

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

     

     
      3、后台URL规则中添加:
     
      
    1. url示例:1/ 
    2.  
    3. url规则:{$categorydir}{$catdir}/|{$categorydir}{$catdir}/{$page}/ 

     

     
      更新栏目缓存就OK了。