shershennm / yii2-seo
用于简单生成关键词和描述的Yii2扩展
3.1.1
2015-10-19 00:00 UTC
Requires
- php: >=5.4.0
- yiisoft/yii2: ^2.0.13
This package is auto-updated.
Last update: 2024-08-29 03:35:56 UTC
README
用于轻松创建元标签的Yii2模块
安装
composer require shershennm/yii2-seo:"^3.0"
用法
在配置文件中
<?php ... 'bootstrap' => ['log', 'seo'], // add seo component to application bootstrap ... 'components' => [ ... 'seo' => [ 'class' => 'shershennm\seo\Seo', 'controllerMapping' => [ 'app\controllers' => 'app\seo\controllers', // controller namespace for seo module ], ], ]
Seo控制器示例
<?php namespace app\seo\controllers; use Yii; use shershennm\seo\SeoController; class SiteController extends SeoController { /** * $viewParams array View Params from actionIndex in SiteController **/ public function actionIndex($viewParams) { $this->title = 'Hello world!'; $this->registerMetaTag(['name' => 'description', 'content' => 'Cool page!']); $this->registerLinkTag([['rel' => 'next', 'href' => 'https://my-cool-page.lh/article/2']]); return [ ['name' => 'keywords', 'content' => $this->getKeywords()], // params for View::registerMetaTag() function ['name' => 'description', 'content' => 'Cool page!'], ]; } private function getKeywords() { // $this->controller instance of current controller return implode($this->controller->words, ', '); } ....
您可以使用 OnePagSeoController 用于具有不同路由的 index 操作的控制器。示例
<?php namespace frontend\seo\controllers; use shershennm\seo\OnePageSeoController; class SiteController extends OnePageSeoController { protected $titles = [ 'site/info' => 'Site Info', ]; protected $wildcardTitles = [ '/site\/history/' => 'Site History', ]; }
$titles 的路由将仅应用于具有相同路由的页面。 $wildcardTitles 使用正则表达式作为路由。