完善前台seo功能
This commit is contained in:
@@ -17,6 +17,7 @@ use app\model\Config;
|
||||
use think\facade\Route;
|
||||
use think\facade\Event;
|
||||
use app\model\Hooks;
|
||||
use app\model\SeoRule;
|
||||
|
||||
class Base {
|
||||
|
||||
@@ -216,4 +217,27 @@ class Base {
|
||||
$doc = new \doc\Doc();
|
||||
return $doc->parse($text);
|
||||
}
|
||||
|
||||
protected function setSeo($title = '', $keywords = '', $description = '') {
|
||||
$seo = array(
|
||||
'title' => $title,
|
||||
'keywords' => $keywords,
|
||||
'description' => $description,
|
||||
);
|
||||
//获取还没有经过变量替换的META信息
|
||||
$meta = SeoRule::getMetaOfCurrentPage($this->request, $seo);
|
||||
foreach ($seo as $key => $item) {
|
||||
if (is_array($item)) {
|
||||
$item = implode(',', $item);
|
||||
}
|
||||
$meta[$key] = str_replace("[" . $key . "]", $item . '|', $meta[$key]);
|
||||
}
|
||||
|
||||
$data = array(
|
||||
'title' => $meta['title'],
|
||||
'keywords' => $meta['keywords'],
|
||||
'description' => $meta['description'],
|
||||
);
|
||||
View::assign('seo', $data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,10 @@ use \app\controller\Base as BaseC;
|
||||
|
||||
class Base extends BaseC {
|
||||
|
||||
protected function initialize(){
|
||||
|
||||
}
|
||||
|
||||
protected function fetch($template = '') {
|
||||
$config = Cache::get('system_config_data');
|
||||
$this->tpl_config['view_depr'] = '_';
|
||||
|
||||
@@ -44,6 +44,8 @@ class Content extends Base {
|
||||
'list' => $list,
|
||||
'page' => $list->render()
|
||||
];
|
||||
|
||||
$this->setSeo($this->modelInfo['title'] . '频道', $this->modelInfo['title'] . '频道', $this->modelInfo['title'] . '频道');
|
||||
return $this->fetch($teamplate);
|
||||
}
|
||||
|
||||
@@ -84,6 +86,7 @@ class Content extends Base {
|
||||
'list' => $list,
|
||||
'page' => $list->render()
|
||||
];
|
||||
$this->setSeo($cate['title'], $cate['title'], $cate['title']);
|
||||
return $this->fetch($teamplate);
|
||||
}
|
||||
|
||||
@@ -130,6 +133,7 @@ class Content extends Base {
|
||||
'prev' => $prev,
|
||||
'next' => $next
|
||||
];
|
||||
$this->setSeo($detail['title'], $detail['title'], $detail['title']);
|
||||
return $this->fetch($teamplate);
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ class Index extends Base {
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function index() {
|
||||
$this->setSeo("网站首页", '网站首页', '网站首页');
|
||||
return $this->fetch();
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ class SeoRule extends \think\Model {
|
||||
public static $keyList = [
|
||||
['name' => 'id', 'title' => '标识', 'type' => 'hidden'],
|
||||
['name' => 'title', 'title' => '规则名称', 'type' => 'text', 'option' => '', 'help' => '规则名称,方便记忆'],
|
||||
['name' => 'app', 'title' => '模块名', 'type' => 'select', 'option' => [['key'=>'*', 'label' => '-所有模块-'], ['key'=>'index', 'label' => '前台模块'], ['key'=>'user', 'label' => '用户中心']], 'help' => '不选表示所有模块'],
|
||||
['name' => 'app', 'title' => '模块名', 'type' => 'select', 'option' => [['key'=>'*', 'label' => '-所有模块-'], ['key'=>'front', 'label' => '前台模块'], ['key'=>'user', 'label' => '用户中心']], 'help' => '不选表示所有模块'],
|
||||
['name' => 'controller', 'title' => '控制器', 'type' => 'text', 'option' => '', 'help' => '不填表示所有控制器'],
|
||||
['name' => 'action', 'title' => '方法', 'type' => 'text', 'option' => '', 'help' => '不填表示所有方法'],
|
||||
['name' => 'seo_title', 'title' => 'SEO标题', 'type' => 'text', 'option' => '', 'help' => '不填表示使用默认'],
|
||||
@@ -55,8 +55,7 @@ class SeoRule extends \think\Model {
|
||||
return $data['app'] . '/' . $data['controller'] . '/' . $data['action'];
|
||||
}
|
||||
|
||||
public function getMetaOfCurrentPage($seo) {
|
||||
$request = \think\Request::instance();
|
||||
public static function getMetaOfCurrentPage($request, $seo) {
|
||||
foreach ($seo as $key => $value) {
|
||||
if (is_array($value)) {
|
||||
$seo_to_str[$key] = implode(',', $value);
|
||||
@@ -64,13 +63,20 @@ class SeoRule extends \think\Model {
|
||||
$seo_to_str[$key] = $value;
|
||||
}
|
||||
}
|
||||
$result = $this->getMeta($request->module(), $request->controller(), $request->action(), $seo_to_str);
|
||||
if (false !== strrpos($request->controller(), ".")) {
|
||||
list($module, $controller) = explode(".", $request->controller());
|
||||
}else{
|
||||
$module = "front";
|
||||
$controller = $request->controller();
|
||||
}
|
||||
|
||||
$result = self::getMeta($module, $controller , $request->action(), $seo_to_str);
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function getMeta($module, $controller, $action, $seo) {
|
||||
private static function getMeta($module, $controller, $action, $seo) {
|
||||
//获取相关的规则
|
||||
$rules = $this->getRelatedRules($module, $controller, $action);
|
||||
$rules = self::getRelatedRules($module, $controller, $action);
|
||||
|
||||
//按照排序计算最终结果
|
||||
$title = '';
|
||||
@@ -112,11 +118,11 @@ class SeoRule extends \think\Model {
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function getRelatedRules($module, $controller, $action) {
|
||||
private static function getRelatedRules($module, $controller, $action) {
|
||||
//查询与当前页面相关的SEO规则
|
||||
$rules = $this->where('app', ['=', '*'], ['=', $module], 'or')
|
||||
->where('controller', ['=', '*'], ['=', $controller], 'or')
|
||||
->where('action', ['=', '*'], ['=', $action], 'or')
|
||||
$rules = self::where('app', 'like', ['*', $module], 'or')
|
||||
->where('controller', 'like', ['*', $controller], 'or')
|
||||
->where('action', 'like', ['*', $action], 'or')
|
||||
->where('status', 1)
|
||||
->order('sort asc')
|
||||
->select();
|
||||
|
||||
Reference in New Issue
Block a user