Seo meta contents behavior for Yii 2
This extension provides behavior functions for seo meta tags and title tag support.
Also provides view helper for registering meta tags and title., (*1)
Installation
My favorite way to install this extension is through composer., (*2)
Either run, (*3)
$ composer require romi45/yii2-seo-behavior:~1.0
or add, (*4)
"romi45/yii2-seo-behavior": "~1.0"
to the require
section of your composer.json
file., (*5)
and then run migration, (*6)
php yii migrate --migrationPath="@vendor/romi45/yii2-seo-behavior/migrations"
Configuring
First you need to configure your model:, (*7)
use romi45\seoContent\components\SeoBehavior;
class Post extends ActiveRecord
{
/**
* @inheritdoc
*/
public function behaviors() {
return [
[
'seo' => [
'class' => SeoBehavior::className(),
// This is default values. Usually you can not specify it
'titleAttribute' => 'seoTitle',
'keywordsAttribute' => 'seoKeywords',
'descriptionAttribute' => 'seoDescription'
],
],
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
// ...
[['seoTitle', 'seoKeywords', 'seoDescription'], 'safe'],
[['seoTitle'], 'checkSeoTitleIsGlobalUnique'], // It recommends for title to be unique for every page. You can ignore this recommendation - just delete this rule.
// ...
];
}
}
Now you are ready to use it on form, (*8)
= $form->field($model, 'seoTitle')->textInput(); ?>
= $form->field($model, 'seoKeywords')->textInput(); ?>
= $form->field($model, 'seoDescription')->textarea(); ?>
As you can see, seoTitle
, seoKeywords
and seoDescription
is the attributes (by default) from which we can access SEO content of model., (*9)
Once you post a form with the above fields, they will be automatically saved and linked to our Post
model., (*10)
To register meta tags and set title in view use following code:, (*11)
use romi45\seoContent\components\SeoContentHelper;
/**
* You can also user partial register functions
* @see SeoContentHelper::registerAll()
*/
SeoContentHelper::registerAll($model);
Do not forget about title tag in layout., (*12)
<title><?= Html::encode($this->title) ?></title>
Patterns
You can use patterns in values and it will replaced with some model properties, application config
property, application parameter or view parameter type will defined by prefixes., (*13)
Model Attribute, (*14)
%%model_ATTRIBUTE_NAME%%
For example %%model_title%%
will replace with php $model->title
, (*15)
Application Global Config Attribute, (*16)
%%appConfig_ATTRIBUTE_NAME%%
For example %%appConfig_name%%
will replace with php Yii::$app->name
, (*17)
Application Global Parameter Attribute, (*18)
%%appParam_ATTRIBUTE_NAME%%
For example %%appParam_contactEmail%%
will replace with php Yii::$app->params['contactEmail'']
, (*19)
View Global Parameter Attribute, (*20)
%%viewParam_ATTRIBUTE_NAME%%
For example %%viewParam_contactEmail%%
will replace with php Yii::$app->view->params['contactEmail'']
., (*21)
Separator, (*22)
%%sep%%
By default separator pattern replaced with '-'. If you want to use another value for separator you need to identify
php Yii::$app->view->params['titleSeparator'']
param., (*23)
Hint: instead of 'titleSeparator' you can use romi45\seoContent\components\SeoPatternHelper::SEPARATOR_VIEW_PARAMETER_KEY
constant value., (*24)
Global Seo Pattern
You can use global seo pattern to all model istance by set is_global
parameter to to 1
for model seoContent
.
Just create form in view like this:, (*25)
<?php $form = \yii\widgets\ActiveForm::begin(); ?>
<?= $form->field($model, 'seoTitle')->textInput(); ?>
<?= $form->field($model, 'seoKeywords')->textInput(); ?>
<?= $form->field($model, 'seoDescription')->textarea(); ?>
<?php \yii\widgets\ActiveForm::end(); ?>
And process it at you controller like this:, (*26)
/* @var $model Page */
$model = new Page();
/* @var $seo SeoContent */
$seo = $model->getSeoContentModel();
if ($seo->load(Yii::$app->request->post())) {
$seo->is_global = 1;
$seo->save();
}
Sql Caching
For enable sql queries caching set enableSqlQueryCache
parameter
at behavior config to to true
. Also you can set
cache duration by sqlQueryCacheDuration
parameter. Example:, (*27)
/**
* @inheritdoc
*/
public function behaviors() {
return [
'seo' => [
'class' => SeoBehavior::className(),
'enableSqlQueryCache' => true,
'sqlQueryCacheDuration' => 24*60*60*30*12, // 1 year
]
];
}
License
The MIT License (MIT). Please see License File for more information., (*28)