1. Go to this page and download the library: Download demi/seo library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
public function behaviors()
{
return [
'seo' => [
'class' => 'demi\seo\SeoModelBehavior',
'seoConfig' => [
'urlField' => 'seo_url',
'metaField' => 'seo_meta',
// attribute name or anonymous function
'urlProduceField' => 'title',
// attribute name or anonymous function
'titleProduceFunc' => 'title',
// attribute name or anonymous function
'descriptionProduceFunc' => 'short_desc',
// attribute name or anonymous function
'keysProduceFunc' => static function (self $model) {
return $model->title . ', tag1, tag2';
},
// when user can manage model seo-fields (anonymous function possible)
'clientChange' => Yii::$app->has('user') && Yii::$app->user->can(User::ROLE_ADMIN),
// param for Url::to(<viewRoute>)
'viewRoute' => '/post/view',
// param for Url::to(<viewRoute>, ['title' => $model->seo_url])
'linkTitleParamName' => 'title',
// only anon-function returns array of additional link params with values
'additionalLinkParams' => static function (self $model) {
// Url::to(<viewRoute>, ['title' => $model->seo_url, 'category' => $model->category->seo_url])
return ['category' => $model->category->seo_url];
},
// if you model have some unique condition
'uniqueUrlFilter' => function (\yii\db\ActiveQuery $query) {
$query->andWhere(['category_id' => $this->category_id]);
},
// if array - seo_meta will have possible to internationalization
'languages' => 'en',
// Optional. All controller actions will added to stop-list for seo_url value.
// For example: if you create model with seo_url = 'delete' you can't open model by url '/post/delete',
// if this option enabled, then seo_url will be 'delete_' and url: '/post/delete_'
'controllerClassName' => '\frontend\controllers\PostController',
// @see [\demi\seo\SeoModelBehavior] for more properties
],
],
];
}
/**
* @property-read array $seoData
* @method array getSeoData($lang = null) Metadata for this model
* @method \demi\seo\SeoModelBehavior getSeoBehavior()
* @property-read array $viewUrl
* @method array getViewUrl() URL to material view page
* @property-read array $absoluteViewUrl
* @method array getAbsoluteViewUrl() Absolute URL to material view page
*/
// set SEO:meta data for current page
$this->setSeoData($model->getSeoBehavior());
// Helper: set <link> tag for "no index" (and optional "no follow") for current page
$this->noIndex($and_no_follow_bool);
public function actionView($title)
{
$model = Post::find()->where(['seo_url' => $title])->one();
if (!$model) {
throw new NotFoundHttpException('Post not found');
}
return $this->render('view', [
'model' => $model,
]);
}
// And in actionCreate() and actionUpdate() change
return $this->redirect(['view', 'id' => $model->id]);
// to
return $this->redirect($model->viewUrl);
// return url to material view page: '/post/super-post'
$url = $model->getViewUrl();
$url = $model->viewUrl;
// return absolute url to material view page: 'http://example.com/post/super-post'
$abs_url = $model->getAbsoluteViewUrl();
$abs_url = $model->absoluteViewUrl;
// Behind scene (for understanding):
return Url::to([$viewRoute, ['title' => $model->$titleField] + $additionalLinkParams]], $isAbsolute);