shershennm / yii2-seo
Yii2 extension for simple generating keywords and description
Installs: 25 699
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 2
Forks: 2
Open Issues: 0
Type:yii2-extension
Requires
- php: >=5.4.0
- yiisoft/yii2: ^2.0.13
This package is auto-updated.
Last update: 2025-01-29 04:31:42 UTC
README
Yii2 module for easy creating meta tags
Installation
composer require shershennm/yii2-seo:"^3.0"
Usage
In config file:
<?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 controller example:
<?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, ', '); } ....
You can use OnePagSeoController
for controller which have index
action for different routes. Example:
<?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', ]; }
Route of $titles
will be applied only to pages with same route. $wildcardTitles
use regular expression as route.