第一步:后台需要开启伪静态
第二步:执行SQL语句,创建tag标签 id表
mysql语句
CREATE TABLE `ay_tag_ex` (`id` int(11) NOT NULL AUTO_INCREMENT,`tag` varchar(255) COLLATE utf8_unicode_ci NOT NULL,PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
sqlite语句
create table ay_tag_ex ([id] integer PRIMARY KEY autoincrement, -- 设置主键[tag] varchar (50));
第三步:在模型添加保存数据的方法
找到 /apps/admin/model/content/TagsModel.php
文件,在文件最后一个{
前面增加如下代码
// <!-- tag修改 -->public function getTagId($tag){$one = parent::table('ay_tag_ex')->where("tag='{$tag}'")->find();if ($one){$tag = $one->id;}return $tag;}public function getTagName($id){$tag = '';$one = parent::table('ay_tag_ex')->where("id={$id}")->find();if ($one){$tag = $one->tag;}return $tag;}public function saveTagEx($tags){$tags_arr = explode(',',$tags);foreach ($tags_arr as $tag) {$one = parent::table('ay_tag_ex')->where("tag='{$tag}'")->find();if (empty($one)){$add = ['tag'=>$tag];parent::table('ay_tag_ex')->insert($add);}}}public function saveAllTagEx(){$contents = parent::table('ay_content')->select();foreach ($contents as $content) {$this->saveTagEx($content->tags);}return true;}
第四步:修改文章内容新增和修改保存逻辑
打开文件 /apps/admin/controller/content/ContentController.php
,找到如下代码
namespace appdmincontrollercontent;
在下方添加
use appdminmodelcontentTagsModel;
继续找到
if (! ! $id = $this->model->addContent($data)) {
在下方添加
// <!-- tag修改 -->$tgMd = new TagsModel();$tgMd->saveTagEx($tags);$tgMd->saveAllTagEx();
继续找到
if ($this->model->modContent($id, $data)) {
在下方添加
// <!-- tag修改 -->$tgMd = new TagsModel();$tgMd->saveTagEx($tags);$tgMd->saveAllTagEx();
完成上面几步,添加和修改文章,就会有映射了,也就是说 ay_tag_ex
表会有id和标签名的对应关系了。
第五步:修改前端访问tag的逻辑
打开文件 /apps/home/controller/ParserController.php
, 找到如下代码
namespace apphomecontroller;
在下方添加
use appdminmodelcontentTagsModel;
继续找到如下代码
if ($fuzzy) {$where2[] = "a.tags like '%" . $get_tag . "%'";} else {$where2[] = "a.tags='" . $get_tag . "'";}
替换成
// <!-- tag修改 -->list($tid,$tml) = explode('.',$get_tag);$tgMd = new TagsModel();$tag_name =$tgMd->getTagName($tid);if ($fuzzy) {$where2[] = "a.tags like '%" . $tag_name . "%'";} else {$where2[] = "a.tags='" . $tag_name . "'";}
继续搜索找到如下代码(大概1840行
)
$url_rule_type = $this->config('url_rule_type') ?: 3;
在下方添加
// <!-- tag修改 -->$tagMd = new TagsModel();$id = $tagMd->getTagId($value['tags']);$idTag = $id.".html";
并将 urlencode($value['tags'])
修改成 $idTag
继续找到
$link = $link . '?tag=' . $idTag;
修改成
// <!-- tag修改 -->$link = $link . 'tag/' . $idTag;
第六步:修改TagController.php文件
打开 /apps/home/controller/TagController.php
文件,找到如下代码(大概33行
)
if (defined('RVAR')) {$_GET@['tag'] = RVAR;}
在下方添加
// <!-- tag修改 -->$tag_arr = explode('.',$_GET@['tag']);list($id,$p) = explode('_', $tag_arr[0]);$_GET@['tag'] = $id.".html";$_GET@['page'] = $p ? $p : 1;$tagMd = new ppdminmodelcontentTagsModel();$name = $tagMd->getTagName($id);$_GET@['tag_name'] = $name;
第七步:修改 coreiewPaging.php
文件(处理分页)
找到
if ($page == 1) {if ($qs) {return $this->getPreUrl() . '?' . $qs;} else {return $this->getPreUrl();}} else {if ($qs) {return $this->getPreUrl() . '?' . $qs . '&page=' . $page;} else {return $this->getPreUrl() . '?page=' . $page;}}
替换成
if ($page == 1) {if ($qs) {return $this->getPreUrl() . '?' . $qs;} else {if(strpos($this->getPreUrl(),'/tag/') !== false){ //判断是否tag页面$pattern="/(_d+)/i";$r="";$a = preg_replace($pattern,$r,$this->getPreUrl());$a = str_replace(".html", "", $a) . '.html';return $a;}else{return $this->getPreUrl();}}} else {if ($qs) {return $this->getPreUrl() . '?' . $qs . '&page=' . $page;} else {if(strpos($this->getPreUrl(),'/tag/') !== false){$pattern="/(_d+)/i";$r="";$a = preg_replace($pattern,$r,$this->getPreUrl());$a = str_replace(".html", "", $a) . '_' . $page.'.html';return $a;}else{return $this->getPreUrl() . '?page=' . $page;}}}
第八步:修改IndexController.php
打开 ppshomecontrollerIndexController.php
,找到
if ($url_rule_type == 2 && stripos(URL, $_SERVER['SCRIPT_NAME']) !== false) { // 禁止伪静态时带index.php访问_404('您访问的内容不存在,请核对后重试!');}$path = P;
修改成
if (stripos(URL, '/tag/') !== false){$arr = explode('/',URL);$p = $arr[0].$arr[1];$path = "/".$p."/";foreach ($arr as $k=>$val) {if ($val=='tag'){break;}}$tag_v = $arr[++$k];$parr = explode('_',rtrim($tag_v,'.html'));if (!strpos(URL,'.html')){$url = ((int)$_SERVER['SERVER_PORT'] == 80 ? 'http://' : 'https://') . $_SERVER['HTTP_HOST'];$url = $url.rtrim(URL,'/');//替换if (isset($parr[1])){$path2 = "/".$p."_".$parr[1]."/";$url = str_replace($path,$path2,$url);}else{//获取当前类目$sort = $this->model->getSort($p);if (!$sort){//去掉$p2 = strstr($p,"_",true);$path2 = "/".$p2."/";$url = str_replace($path,$path2,$url);}}$url = str_replace($tag_v,$parr[0] .'.html',$url);header('Location: '.$url);}$_GET@['page'] = $parr[1] ? $parr[1] : 1;$_GET@['tag'] =$parr[0] .'.html';$tagMd = new ppdminmodelcontentTagsModel();$name = $tagMd->getTagName($parr[0]);$_GET@['tag_name'] = $name;}else{if ($url_rule_type == 2 && stripos(URL, $_SERVER['SCRIPT_NAME']) !== false) { // 禁止伪静态时带index.php访问_404('您访问的内容不存在,请核对后重试!');}$path = P;}
tag列表调佣tag标签名称
{$get.tag_name}
完成。