Skip to main content
 首页 » 程序教程

phpcms v9怎么目录式伪静态

2015年12月12日49930

phpcmsV9栏目伪静态的修改方法(支持自定义目录名、自定义文件名),官方程序默认伪静态是不支持自定义栏目名的,所以今天就做了以下修改, 让其支持!phpcms v9的伪静态一般来说,就是两个方面,一方面是URL路径显示为静态形式,这个通过phpcms的URL规则即可实现;另一个方面是通过Web服务器对链 接进行重写。

URL规则部分,使用phpcms的大多是知道的,就不再赘述。如果需要支持自定义文件名的,需要去看看phpcms v9自定义HTML文件名一文。下面就着重将Web服务器重写链接这一项。

phpcms v9怎么目录式伪静态

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

rewrite ^/([a-z]+)/$ /index.php?m=content&c=index&a=lists&catdir=$1 last;
rewrite ^/([a-z]+)/index_(\d+)\.html$ /index.php?m=content&c=index&a=lists&catdir=$1&page=$2 last;
rewrite ^/([a-z]+)/([a-z0-9-]+)\.html$ /index.php?m=content&c=index&a=show&catdir=$1&prefix=$2 last;
rewrite ^/([a-z]+)/([a-z0-9-]+)_(\d+)\.html$ /index.php?m=content&c=index&a=show&catdir=$1&prefix=$2&page=$3 last;

这个Nginx规则就是把后面的链接重写为前面的链接形式的。所以接下来需要做的就是让后面的链接能够访问到正确的页面。phpcms v9默认是无法访问到正确页面的,因为缺少必要的参数。下面就对phpcms v9进行修改。

1、打开phpcms\modules\content目录下的index.php找到 public function lists() {,将$catid = intval($_GET['catid']);替换成:

if(isset ($_GET['catid'])){
   $catid = intval($_GET['catid']);
}else{
   $catid=$this->_getCategoryId($_GET['catdir']);
}

并且在最后的}?> 前添加:

private function _getCategoryId($catdir){
    if(!strpos($catdir,'/')) {
        $dirname = $catdir;
    }else {
        $dirname = end(explode('/',$catdir));
    }
        $this->category_db = pc_base::load_model('category_model');
        $result = $this->category_db->get_one(array('catdir'=>$dirname));
    return $result['catid'];
    }

这段代码的作用就是判断参数是否catdir,栏目自定义目录名,如果是目录名则根据目录名得到对应的catid,从而顺利得到栏目页。理解了自定义目录的伪静态方法之后,自定义文件名的伪静态也就可以类似解决。

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

$catid = intval($_GET['catid']);
$id = intval($_GET['id']);

替换成:

if(isset ($_GET['catid'])){
    $catid = intval($_GET['catid']);
}else{
    $catid=$this->_getCategoryId($_GET['catdir']);
}
if(isset($_GET['id'])){
    $id = intval($_GET['id']);
}else{
    $id = $this->_getPrefixId($_GET['catdir'],$_GET['prefix']);
}

跟上面一样,还需要加上判断出文章id的函数,函数如下:

private function _getPrefixId($catdir,$prefix){
    if(!strpos($catdir,'/')) {
        $dirname = $catdir;
    }else {
        $dirname = end(explode('/',$catdir));
    }
    $this->category_db = pc_base::load_model('category_model');
    $result = $this->category_db->get_one(array('catdir'=>$dirname));
    $catid = $result['catid'];
    $siteids = getcache('category_content','commons');
    $siteid = $siteids[$catid];
    $CATEGORYS = getcache('category_content_'.$siteid,'commons');
    if(!isset($CATEGORYS[$catid]) || $CATEGORYS[$catid]['type']!=0) showmessage(L('information_does_not_exist'),'blank');
    $this->category = $CAT = $CATEGORYS[$catid];
    $this->category_setting = $CAT['setting'] = string2array($this->category['setting']);
    $siteid = $GLOBALS['siteid'] = $CAT['siteid'];
    $MODEL = getcache('model','commons');
    $modelid = $CAT['modelid'];
    $tablename = $this->db->table_name = $this->db->db_tablepre.$MODEL[$modelid]['tablename'];
    $result = $this->db->get_one(array('prefix'=>$prefix));
    if(empty($result)){
        $result = $this->db->get_one(array('id'=>$prefix));
    }
    return $result['id'];
}

到这里之后,就可以通过自定义目录加自定义文件名的形式访问了。其他一些细节的地方,就不再赘述。

评论列表暂无评论
发表评论