yiier / yii2-seo
Library for working with SEO parameters of models
Installs: 49
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 4
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- yiisoft/yii2: ~2.0.0
This package is auto-updated.
Last update: 2024-11-06 17:58:25 UTC
README
Library for working with SEO parameters of models
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist yiier/yii2-seo "*"
or add
"yiier/yii2-seo": "*"
to the require section of your composer.json
file.
Configuration
change your config file
<?php return [ 'components' => [ 'view' => [ 'as seo' => [ 'class' => 'yiier\seo\SeoViewBehavior', 'names' => [ 'keywords' => 'blog,forecho', 'author' => getenv('APP_NAME'), ], 'properties' => [ [ 'property' => ['title', 'og:title'], 'content' => function () { return ' tag1, tag2'; }, ], 'title1' => 'title' ], ] ] ] ];
In model file add seo model behavior:
<?php public function behaviors() { return [ 'seo' => [ 'class' => 'yiier\seo\SeoModelBehavior', 'names' => [ 'viewport' => function (self $model) { return $model->title . ', tag1, tag2'; }, 'keywords' => 'blog,forecho', 'author' => 'author', // model field ], 'properties' => [ [ 'property' => ['title', 'og:title'], 'content' => function (self $model) { return $model->title . ', tag1, tag2'; }, ], 'title1' => 'title', [ 'property' => 'description', 'content' => function (self $model) { return $model->title . ', tag1, tag2'; }, ], ], ], ]; }
PHPdoc for model:
/**
* @property \yiier\seo\SeoModelBehavior $seoBehavior
* @method \yiier\seo\SeoModelBehavior getSeoBehavior()
*/
Change /frontend/views/layouts/main.php
:
<?php /* @var $this \yii\web\View|\yiier\seo\SeoViewBehavior */ ?> <head> <!-- Replace default <title> tag --> <title><?= Html::encode($this->title) ?></title> <!-- by this line: --> <?php $this->renderMetaTags(); ?> ... </head>
Usage
In "view.php" file for model:
// set SEO:meta data for current page $this->setSeoData($model->getSeoBehavior());
or in controller:
Yii::$app->view->setSeoData($model->getSeoBehavior());
Simple url rules example in '/frontend/config/main.php':
'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [ '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>', '<module>/<controller:\w+>/<action:\w+>/<id:\d+>' => '<module>/<controller>/<action>', 'post/<action:(index|create|update|delete)>' => 'post/<action>', 'post/<title:[-\w]+>' => 'post/view', ], ],