laco-agency/seo-behavior

Seo behavior for Yii2 models and meta tags helper

0.4 2018-10-05 09:20 UTC

This package is not auto-updated.

Last update: 2024-06-14 21:34:24 UTC


README

SEO Behavior

Seo behavior for Yii2 models and meta tags helper

Installing

composer require --prefer-dist laco-agency/seo-behavior

or add

"laco-agency/seo-behavior": "*"

to the require section of your composer.json.

Usage

Model:

use laco/seo/SeoModelBehavior;
public function behaviors()
{
    return [
        [
            'class' => SeoModelBehavior::className(),
            'descriptionFromAttribute' => 'teaser',
            'metaImageAttribute' => 'image_preview'
       ]
   ]
}

Controller:

use laco/seo/SeoControllerBehavior;

Attach behavior:

public function behaviors()
{
    return [
        SeoControllerBehavior::className()
   ];
}

In case when parent controller already has behaviors, you can attach SeoControllerBehavior like this:

public function behaviors()
{
    $behaviors = [
        'access' => [
            'class' => AccessControl::className(),
            'rules' => []
        ],
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => []
        ]
    ];
    return array_merge(parent::behaviors(), $behaviors);
}

Or like this:

public function init()
{
    $this->attachBehavior('seo', SeoControllerBehavior::className());
}

Action:

In the action use the method $this->setMetaTags($model) and pass $model as parameter;

public function view($slug)
{
    $model = Material::findOne(['slug' => $slug]));
    $this->setMetaTags($model);
}

Or use an array in this format instead of model:

[
    'metaTitle' => 'Custom meta title',
    'metaDescription' => 'Custom description',
    'metaImage' => 'Custom meta image'
]