laco-agency / seo-behavior
Seo behavior for Yii2 models and meta tags helper
Installs: 25
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- yiisoft/yii2: *
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' ]