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、打开phpcmsmodulescontent目录下的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、打开phpcmsmodulescontent目录下的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'];
}
到这里之后,就可以通过自定义目录加自定义文件名的形式访问了。其他一些细节的地方,就不再赘述
ETF巨头持有6%的比特币——但73K的BTC价格可能突破他们的耐心底线
日报 | 天风证券子公司获香港虚拟资产第三类牌照;Coinbase CEO 称公司每周都在买入更多比特币
美国通过《部署美国区块链法案》:这对加密货币意味着什么
Aptos创16日新高 首支Aptos现货ETF即将问世
L2代币面临估值泡沫与投票操控风险
分析师预测SHIB目标价格为0.000081美元,技术分析显示柴犬币可能迎来600%暴涨