There is no license information available for the latest version (dev-master) of this package.

Yii2 SEO module

Installs: 3 605

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 5

Forks: 1

Open Issues: 3

Type:yii2-extension

dev-master 2021-10-15 08:37 UTC

This package is auto-updated.

Last update: 2024-04-15 14:04:10 UTC


README

SEO-fields for your models: title, description, keywords and some others

Install

Run

php composer require pantera-digital/yii2-seo "*"

Or add to composer.json

"pantera-digital/yii2-seo": "*",

and execute:

php composer update

Миграция:

php yii migrate --migrationPath=vendor/pantera-digital/yii2-seo/migrations

Настройка

Модуль зависит от https://github.com/yiisoft/yii2-twig для полной работы пресетов нужно сконфигурировать это расширение

Usage

Attach behavior to your model:

    function behaviors()
    {
        return [
            'seo' => [
                'class' => 'pantera\seo\behaviors\SeoFields',
            ],
        ];
    }

Example of use in view:

if (!$title = $model->seo->title) {
    $title = "Buy {$model->name} in store";
}

if (!$description = $model->seo->description) {
    $description = 'Page ' . $model->name;
}

if (!$keywords = $model->seo->keywords) {
    $keywords = '';
}

$this->title = $title;

$this->registerMetaTag([
    'name' => 'description',
    'content' => $description,
]);

$this->registerMetaTag([
    'name' => 'keywords',
    'content' => $keywords,
]);

Widgets

Add to your model form:

<?=\pantera\seo\widgets\SeoForm::widget([
        'model' => $model, 
        'form' => $form, 
    ]); ?>

Нужно подключить компонент в конфиг

'components' => [
    'seo' => [
        'class' => pantera\seo\components\SeoComponent::className(),
    ],
]

Использование регистрации мета данных по адресу

Нужно подключить в бутстрап приложения класс

'bootstrap' => [pantera\seo\Registrar::className()],

Not found

Для логирования нужно в обработчик ошибок добавить

if ($exception instanceof NotFoundHttpException) {
    $logger = new pantera\seo\models\SeoNotFound();
    $logger->url = Yii::$app->request->url;
    $logger->referrer = Yii::$app->request->referrer;
    $logger->ip = Yii::$app->request->getRemoteIP();
    $logger->save();
}

Slug

В модель нужно добавить поведение

public function behaviors()
{
    return [
        [
            'class' => pantera\seo\behaviors\SlugBehavior::className(),
            'attribute' => 'title',
            'slugAttribute' => 'slug',
        ],
    ];
}

В модель подключить валидатор

public function rules()
{
    return [
        [['slug'], pantera\seo\validators\SlugValidator::className(), 'skipOnEmpty' => false],
    ];
}

Сконфигурировать свой UrlManager

Модуль имеит возможность регистрации Open Graph тегов

нужно сконфигурировать расширение https://packagist.org/packages/dragonjet/yii2-opengraph

'components' => [
    'openGraph' => [
        'class' => dragonjet\opengraph\OpenGraph::class,
    ],
],

В поле Og Image нужно написать как получить картинку используя twig