This extension provides behavior functions for seo meta tags and title tag support. Also provides view helper for registering meta tags and title.
My favorite way to install this extension is through composer.
Either run
$ composer require romi45/yii2-seo-behavior:~1.0
or add
"romi45/yii2-seo-behavior": "~1.0"
to the require
section of your composer.json
file.
and then run migration
php yii migrate --migrationPath="@vendor/romi45/yii2-seo-behavior/migrations"
First you need to configure your model:
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
<?= $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.
Once you post a form with the above fields, they will be automatically saved and linked to our Post
model.
To register meta tags and set title in view use following code:
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.
<title><?= Html::encode($this->title) ?></title>
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.
Model Attribute
%%model_ATTRIBUTE_NAME%%
For example %%model_title%%
will replace with php $model->title
Application Global Config Attribute
%%appConfig_ATTRIBUTE_NAME%%
For example %%appConfig_name%%
will replace with php Yii::$app->name
Application Global Parameter Attribute
%%appParam_ATTRIBUTE_NAME%%
For example %%appParam_contactEmail%%
will replace with php Yii::$app->params['contactEmail'']
View Global Parameter Attribute
%%viewParam_ATTRIBUTE_NAME%%
For example %%viewParam_contactEmail%%
will replace with php Yii::$app->view->params['contactEmail'']
.
Separator
%%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.
Hint: instead of 'titleSeparator' you can use romi45\seoContent\components\SeoPatternHelper::SEPARATOR_VIEW_PARAMETER_KEY
constant value.
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:
<?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:
/* @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();
}
For enable sql queries caching set enableSqlQueryCache
parameter
at behavior config to to true
. Also you can set
cache duration by sqlQueryCacheDuration
parameter. Example:
/**
* @inheritdoc
*/
public function behaviors() {
return [
'seo' => [
'class' => SeoBehavior::className(),
'enableSqlQueryCache' => true,
'sqlQueryCacheDuration' => 24*60*60*30*12, // 1 year
]
];
}
The MIT License (MIT). Please see License File for more information.