drtsb/yii2-seo

Yii2 SEO module.

Installs: 87

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 2

Forks: 0

Open Issues: 1

Type:yii2-extension

0.4.1 2020-05-02 10:10 UTC

This package is auto-updated.

Last update: 2024-11-19 23:05:35 UTC


README

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:

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
                ];
            },
        ],
    ];
}