drtsb/yii2-seo

Yii2 SEO module.


Keywords
module, extension, seo, yii, yii2, yii 2
License
MIT

Documentation

yii2-seo

Yii2 SEO module

Latest Stable Version Total Downloads License Build Status

Installation

The preferred way to install this extension is through composer.

Either run

composer require drtsb/yii2-seo:*

or add

"drtsb/yii2-seo" : "*"

to the require section of your application's composer.json file.

Usage

The extension consists of 2 parts:

  • Module provides a simple CRUD to edit SEO params for pages specified by controller and action.
  • ModelBehavior adds SEO fields to specific model.

First of all, you need to apply migrations

yii migrate --migrationPath=@vendor/drtsb/yii2-seo/src/migrations

or use namespaced migrations and add

'controllerMap' => [
    ...
    'migrate' => [
        'class' => 'yii\console\controllers\MigrateController',
        'migrationNamespaces' => [
            'drtsb\yii\seo\migrations',
        ],
    ],
    ...
],

to your apllication config file.

Module configuration

'modules' => [
	...
    'seo' => [
        'class' => 'drtsb\yii\seo\Module',
        'accessRoles' => ['admin'], // Default: null
        'pagination' => ['pageSize' => 10], // Default: false
    ],
    ...
],

After that, you can manage your SEO by accessing /seo/default

You can use * mask at Controller and Action fields For example, for page site/index will be used first appropriate row in following order:

Order Controller Action Title Meta Description
1 site index Title for site/index ...
2 site * Title for any action of site controller ...
3 * index Title for index action of any controller ...
4 * * Title for any action of any controller ...

Add behavior to your frontend controller

use drtsb\yii\seo\behaviors\SeoBehavior;

public function behaviors()
{
    return [
        SeoBehavior::class,
    ];
}

Working with models

Add behavior to your model

public function behaviors()
{
    return [
        'seo' => [
            'class' => 'drtsb\yii\seo\behaviors\SeoModelBehavior',
        ],
    ];
}

add to model's form

<?= $model->seoFields($form) ?>

Register meta tags at frontend view

use drtsb\yii\seo\widgets\MetaTagsWidget;

MetaTagsWidget::widget(['view' => $this, 'model' => $model]);

You can also generate SEO values depending on model's attributes using dataClosure param

public function behaviors()
{
    return [
        'seo' => [
            'class' => 'drtsb\yii\seo\behaviors\SeoModelBehavior',
            'dataClosure' => function($model) {
                return [
                    'meta_title' => $model->title . ' Title',
                    'meta_description' => $model->title . ' Description',
                    'meta_keywords' => $model->title . ' Keywords',
                    'meta_noindex' => true,
                    'meta_nofollow' => true,
                    'rel_canonical' => 'site/index', // or 'http://some.site'
                    'dont_use_empty' => true, // Default value false
                ];
            },
        ],
    ];
}