ivankff/yii2-meta-tags

DB based model meta data for SEO

Installs: 6

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 7

Type:yii2-extension

v2.0.1 2020-02-26 10:43 UTC

This package is auto-updated.

Last update: 2024-04-26 20:51:42 UTC


README

DB based model meta data for SEO

screenshot

Installation

The preferred way to install this extension is through composer.

Either run

composer.phar require --prefer-dist v0lume/yii2-meta-tags "*"

or add

"v0lume/yii2-meta-tags": "*"

to the require section of your composer.json file.

Usage

Add MetaTagBehavior to your model, and configure it.

public function behaviors()
{
    return [
        'MetaTag' => [
            'class' => MetaTagBehavior::className(),
        ],
    ];
}

Add MetaTags somewhere in you application, for example in editing form.

echo MetaTags::widget([
    'model' => $model,
    'form' => $form
]);

Done! Now, you can get meta data of your current model:

echo $model->getBehavior('MetaTag')->title;
echo $model->getBehavior('MetaTag')->keywords;
echo $model->getBehavior('MetaTag')->description;

Or, by manually find model:

use v0lume\yii2\metaTags\model\MetaTag;

...

$meta_tag = MetaTag::findOne([
    'model_id' => $id,
    'model'  => (new \ReflectionClass($model))->getShortName()
]);

...

echo $meta_tag->title;
echo $meta_tag->keywords;
echo $meta_tag->description;

Auto registration meta tags

You can use MetaTagsComponent to perform auto registration meta tags

Configure MetaTagsComponent in main.php config:

...
'components' => [
    ...
    'metaTags' => [
        'class' => 'v0lume\yii2\metaTags\MetaTagsComponent',
        'generateCsrf' => false,
        'generateOg' => true,
    ],
    ...
],
...
    

And then, in your layouts or views or controller action

$model = \common\models\Page::findOne(['url' => '/']);

Yii::$app->metaTags->register($model);

If passed $model was attached MetaTagBehavior, component will register meta tags for that model. If MetaTagBehavior wasn't attached or model not passed, and generateCsrf is set to true, component will generate only csrf meta tags.