laco-agency / seo-behavior
Seo behavior for Yii2 models and meta tags helper
Package info
github.com/laco-agency/seo-behavior
Type:yii2-extension
pkg:composer/laco-agency/seo-behavior
0.4
2018-10-05 09:20 UTC
Requires
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2026-03-07 05:42:56 UTC
README
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'
]