用栏目标识来访问cms模块具体修改地方

1.首先前台路由改下,需要在各文章列表增加subtitle字段属性跟title一样

Route::rule('lists/:catid/[:condition]', 'cms/index/lists')->pattern(['catid' => '[d|w/]+', 'condition' => '[0-9_&=a-zA-Z]+']);
Route::rule('shows/:catid/:id', 'cms/index/shows')->pattern(['catid' => '[d|w/]+', 'id' => '[d|w/]+']);

2./application/cms/controller/index.php 文件修改

1.) public function lists()函数修改

//$catid = $this->request->param('catid/d', 0);
        $catid = $this->request->param('catid/w', 0);
        $page = $this->request->param('page/d', 1);
        //获取栏目数据
        //$category = getCategory($catid);
        if (is_numeric($catid))
        {
            $category = getCategory($catid);
        }
        else{
            $category = getCategoryByName($catid);
            $catid = $category['id'];
        }

2.)public function shows() 函数修改


//$id = $this->request->param('id/d', 0);
        $id = $this->request->param('id', 0);
        //栏目ID
        //$catid = $this->request->param('catid/d', 0);
        $catid = $this->request->param('catid', 0);
        $page = $page = $this->request->param('page/d', 1);
        //获取栏目数据
        //$category = getCategory($catid);
        if (is_numeric($catid))
        {
            $category = getCategory($catid);
        }
        else
        {
            $category = getCategoryByName($catid);
        }
$modelInfo = cache('Model')[$modelid];
    //后增加的
        if (!is_numeric($id))
        {
            $id = Db::name($modelInfo['tablename'])->where('subtitle', $id)->value("id");
             
             
        }
         
        //更新点击量
        Db::name($modelInfo['tablename'])->where('id', $id)->setInc('hits');



3.application/cms/model/Category.php  文件

$this->assign([
            'top_parentid' => $top_parentid,
            'SEO' => $seo,
            'catid' => $catid,
            'image' => $image,
            'parentid' => $parentid,
            'page' => $page,
            'modelid' => $modelid,
        ]);
        //后增加的
        $this->assign("catid",$category['id']);
        $this->assign("id",$id);


1.)修改public static function getCategory()函数

//$cache['url'] = self::buildCatUrl($cache['type'], $catid, $cache['url']);
         //修改的地方
         $cache['url'] = self::buildCatUrl($cache['type'], $cache['catdir']?$cache['catdir']:$catid, $cache['url']);

2.)增加public static function getCategoryByName() 函数

 public static function buildCatUrl($type, $id, $url = '')
    {
        switch ($type) {
            case 3: //自定义链接
                $url = empty($url) ? '' : ((strpos($url, '://') !== false) ? $url : url($url));
                break;
            default:
                $url = url('cms/index/lists', ['catid' => $id]);
                break;
        }
        //后增加的
        $url = str_replace("%2F","/",$url);
        return $url;
    }

3.) 修改 public static function buildCatUrl()函数

public static function getCategoryByName($catid, $field = '', $newCache = false)
    {
        if (empty($catid)) {
            return false;
        }
        $key = 'getCategory_' . $catid;
        //强制刷新缓存
        if ($newCache) {
            Cache::rm($key, null);
        }
        $cache = Cache::get($key);
        if ($cache === 'false') {
            return false;
        }
        if (empty($cache)) {
            //读取数据
            $cache = db('category')->where(['catdir' => $catid])->find();
            if (empty($cache)) {
                Cache::set($key, 'false', 60);
                return false;
            } else {
                //扩展配置
                $cache['setting'] = unserialize($cache['setting']);
                //修改的地方
                $cache['url'] = self::buildCatUrl($cache['type'], $cache['catdir'], $cache['url']);
                //栏目扩展字段
                //$cache['extend'] = $cache['setting']['extend'];
                $cache['image'] = get_file_path($cache['image']);
                Cache::set($key, $cache, 3600);
            }
        }
        if ($field) {
            //支持var.property,不过只支持一维数组
            if (false !== strpos($field, '.')) {
                $vars = explode('.', $field);
                return $cache[$vars[0]][$vars[1]];
            } else {
                return $cache[$field];
            }
        } else {
            return $cache;
        }
    }

4.application/cms/model/Cms.php 文件

1.)修改public function addModelData() 函数

//后增加的
         if($id){
            $catdir= getCategory($catid,'catdir');
            $pinyin= new OvertruePinyinPinyin('OvertruePinyinMemoryFileDictLoader');
            if (!empty($data['title'])){
                $subtitle= $pinyin->permalink($data['title'],'');
                if ($subtitle && $catdir){
                    $subtitle .= $id;
                    DB::name($tablename)->where('id',$id)->update(['subtitle'=>$subtitle]);
                }
            }
        }
        return $id;
if (!empty($result)) {
            $ModelField = cache('ModelField');
            foreach ($result as $key => $vo) {
                $catdir=getCategory($vo['catid'],'catdir');
                $vo = $this->dealModelShowData($ModelField[$modeId], $vo);
                //$vo['url'] = $this->buildContentUrl($vo['catid'], $vo['id'])
                $vo['url'] = $this->buildContentUrl($vo['catid'], $vo['id'], $vo['subtitle'], $vo['title'], $catdir, $tableName );
                $result[$key] = $vo;
            }
        }

3.) 修改public function getContent()函数2.)修改public function getList() 函数

 if (!empty($dataInfo)) {
            $ModelField = cache('ModelField');
            $catdir = getCategory($dataInfo['catid'],'catdir');
            $dataInfo = $this->dealModelShowData($ModelField[$modeId], $dataInfo);
            //$dataInfo['url'] = $this->buildContentUrl($dataInfo['catid'], $dataInfo['id']);
            $dataInfo['url'] = $this->buildContentUrl($dataInfo['catid'], $dataInfo['id'], $dataInfo['subtitle'], $dataInfo['title'], $catdir, $tableName );
        }
public function buildContentUrl($catid, $id, $subtitle='',$title='', $catdir='', $tableName='')
    {
         if (($subtitle || $title) && $catdir){
             $pinyin = new OvertruePinyinPinyin('OvertruePinyinMemoryFileDictLoader');
             if ($subtitle ==''&& !empty($title)){
                 $subtitle=$pinyin->permalink($title,'');
                 if($subtitle && $catdir){
                    $subtitle .= $id;
                    Db::name($tableName)->where('id',$id)->update(['subtitle'=>$subtitle]);
                 }
             }
             $url = url('cms/index/shows', ['catid' => $catdir, 'id' => $subtitle]);
             $url = str_replace("%2F","/",$url);
             return $url;
         }
         $url = url('cms/index/shows', ['catid' => $catid, 'id' => $id]);
         $url = str_replace("%2F","/",$url);
         return $url;
    }

5.application/cms/taglib/CmsTagLib.php文件4.) 修改public function buildContentUrl()函数

修改public function Category()函数

if (!empty($categorys)) {
            foreach ($categorys as &$vo) {
                //$vo['url'] = Category_Model::buildCatUrl($vo['type'], $vo['id'], $vo['url']);
                $vo['url'] = Category_Model::buildCatUrl($vo['type'], $vo['catdir'], $vo['url']);
                $vo['image'] = get_file_path($vo['image']);
                $vo['image2'] = get_file_path($vo['image2']);
            }
        }

1.)修改验证后台验证规则6.application/cms/validate/Category.php文件

 //'catdir|唯一标识' => 'require|alphaNum',
        'catdir|唯一标识' => 'require|alphaCat',



2.) 增加验证函数

public function alphaCat($val)
    {
        if (!preg_match('/^[a-zA-Z0-9/]+$/u',$val))
        {
            return false;
        }
        return true;
    }

3.) 增加验证提示

//定义验证提示
    protected $message = [
        'modelid.number' => '所属模型不得为空',
        'catdir.alphaCat' => '唯一标识只能为数字或字母及左斜杠/',
    ];

7.application/cms/common.php 文件

1.)增加 function getCategoryByName()函数

function getCategoryByName($catid, $field = '', $newCache = false)
{
    return Category_Model::getCategoryByName($catid, $field, $newCache);
}


2.)修改当前位置

function catpos($catid, $symbol = ' > ')
{
    if (getCategory($catid) == false) {
        return '';
    }
    //获取当前栏目的 父栏目列表
    $arrparentid = array_filter(explode(',', getCategory($catid, 'arrparentid') . ',' . $catid));
    foreach ($arrparentid as $cid) {
        $catdir= getCategory($catid ,'catdir');
        $url = Category_Model::buildCatUrl(getCategory($cid, 'type'), $catdir, getCategory($cid, 'url'));
        $parsestr[] = '<a href="' . $url . '" >' . getCategory($cid, 'catname') . '</a>';
    }
    $parsestr = implode($symbol, $parsestr);
    return $parsestr;
}

可能会有落下的,后续增加,比水月洞天的方法复杂好多,栏目一级标识正常获取,二级标识填写 一级标识/二级标识,三级标识一级标识/二级标识/三级标识 以此类推,外联形式cms/index/lists?catid=Aboutus/Introduction  形式