PHP code example of romi45 / yii2-seo-behavior

1. Go to this page and download the library: Download romi45/yii2-seo-behavior 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/ */

    

romi45 / yii2-seo-behavior example snippets


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.
            // ...
        ];
    }
}


<?= $form->field($model, 'seoTitle')->textInput(); 

use romi45\seoContent\components\SeoContentHelper;

/**
 * You can also user partial register functions
 * @see SeoContentHelper::registerAll()
 */
SeoContentHelper::registerAll($model);

<title><?= Html::encode($this->title) 

%%model_ATTRIBUTE_NAME%%
 $model->title

%%appConfig_ATTRIBUTE_NAME%%
 Yii::$app->name

%%appParam_ATTRIBUTE_NAME%%
 Yii::$app->params['contactEmail'']

%%viewParam_ATTRIBUTE_NAME%%
 Yii::$app->view->params['contactEmail'']

%%sep%%
 Yii::$app->view->params['titleSeparator'']

 $form = \yii\widgets\ActiveForm::begin(); 

/* @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();
}

/**
 * @inheritdoc
 */
public function behaviors() {
    return [
        'seo' => [
            'class' => SeoBehavior::className(),
            'enableSqlQueryCache' => true,
            'sqlQueryCacheDuration' => 24*60*60*30*12, // 1 year
        ]
    ];
}

php yii migrate --migrationPath="@vendor/romi45/yii2-seo-behavior/migrations"