dev-master
9999999-dev https://github.com/nevmerzhitsky/yii2-seomodule#readmeSEO-oriented functional for models and controllers
MIT
The Requires
- php >=5.6.0
- yiisoft/yii2 *
extension yii2 seo
SEO-oriented functional for models and controllers
Add ability to edit content of SEO-oriented HTML tags and attributes. Also add ability to configure redirection from any route to another with 301 status. And more SEO-oriented functions., (*1)
Highly inspired by https://github.com/demisang/yii2-seo and https://github.com/Amirax/yii2-seo-tools., (*2)
The preferred way to install this extension is through composer., (*3)
Either run, (*4)
php composer.phar require nevmerzhitsky/yii2-seomodule "*"
or add, (*5)
"nevmerzhitsky/yii2-seomodule": "*"
to the require section of your composer.json
file. Then run command, (*6)
php composer.phar update
After installation extension run migration:, (*7)
./yii migrate --migrationPath="@vendor/nevmerzhitsky/yii2-seomodule/migrations"
In components configuration add the following:, (*8)
[ 'components' => [ 'seo' => [ 'class' => 'nevmerzhitsky\seomodule\Meta' ], 'view' => [ 'as seo' => [ 'class' => 'nevmerzhitsky\seomodule\SeoViewBehavior', ] ], ], ... ];
And add SEO extension to bootstrap:, (*9)
'bootstrap' => ['log', 'seo']
In model file add seo model behavior:, (*10)
public function behaviors() { $it = $this; return [ 'seo' => [ 'class' => 'nevmerzhitsky\seomodule\SeoModelBehavior', 'title' => [ 'produceFunc' => 'title', 'produceMaxLength' => 150, 'overrideByDb' => false ], 'descriptionProduceFunc' => 'short_desc', 'keysProduceFunc' => function ($model) { /* @var $model self|\yii\db\ActiveRecord */ return $model->title . ', tag1, tag2'; }, 'metaField' => 'seo_meta', 'userCanEdit' => Yii::$app->has('user') && Yii::$app->user->can(User::ROLE_ADMIN), // 'languages' => 'ru', 'urlField' => 'seo_url', 'urlProduceField' => 'title', 'controllerClassName' => '\frontend\controllers\PostController', 'uniqueUrlFilter' => function ($query) use ($it) { /* @var $query \yii\db\Query */ $query->andWhere(['category_id' => $it->category_id]); }, ], ]; }
In main layout:, (*11)
<head> <title><?php echo Html::encode(Yii::$app->seo->title); ?></title> <?php $this->head(); ?> ... </head>
In a controller action for the model ("view" for example):, (*12)
Yii::$app->seo->registerModel($model);
You can register several models in one action. If these models have 'seo' behavior, then SEO data will combined from all of them in registration order., (*13)
In admin/manager site you can add fields for editing SEO data of a model ("_form.php" template):, (*14)
<?php $this->beginContent('@app/vendor/nevmerzhitsky/yii2-seomodule/views/edit-form.php', [ 'model' => $model, 'form' => $form ]); $this->endContent(); ?>
Extension will automatically load the correct row from the database using the currently running and params.You can optionally override data by specifying them in a parameter array, (*15)
Yii::$app->seo->title = 'Page title'; Yii::$app->seo->metakeys = 'seo,yii2,extension'; Yii::$app->seo->metadesc = 'Page meta description'; Yii::$app->seo->tags['og:type'] = 'article';
You can set the templates for tags. For example:, (*16)
Yii::$app->seo->setVar('USER_NAME', 'Smith'); Yii::$app->seo->tags['og:title'] = 'Hello %USER_NAME%';
Default variables: * %HOME_URL% - Homepage url * %CANONICAL_URL% - Canonical URL for current page * %LOCALE% - Site locale, (*17)
For enabling SEO Redirect add to configuration file, (*18)
'errorHandler' => [ 'class' => 'nevmerzhitsky\seomodule\Redirect', ],
SEO-oriented functional for models and controllers
MIT
extension yii2 seo