PHP code example of voskobovich / yii2-seo-toolkit

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

    

voskobovich / yii2-seo-toolkit example snippets


class <ClassName> extends \voskobovich\seo\migrations\create_table__url_route
{

}

class UrlRoute extends \voskobovich\seo\models\UrlRoute
{
    const OBJECT_CATEGORY = 'category';
    const OBJECT_POST = 'post';
    const OBJECT_TAG = 'tag';
    const OBJECT_USER = 'user';

    /**
     * List objects
     * @return array;
     */
    public static function objectItems()
    {
        return [
            static::OBJECT_CATEGORY => 'Category',
            static::OBJECT_POST => 'Post',
            static::OBJECT_TAG => 'Tag',
            static::OBJECT_USER => 'User',
        ];
    }

    /**
     * Route map for objects
     * @return array;
     */
    public static function routeMap()
    {
        return [
            static::OBJECT_CATEGORY => [
                static::ACTION_INDEX => 'category/index',
                static::ACTION_VIEW => 'category/view',
            ],
            static::OBJECT_POST => [
                static::ACTION_INDEX => 'post/index',
                static::ACTION_VIEW => 'post/view',
            ],
            static::OBJECT_TAG => [
                static::ACTION_INDEX => 'tag/index',
                static::ACTION_VIEW => 'tag/view',
            ],
            static::OBJECT_USER => [
                static::ACTION_INDEX => 'user/index',
                static::ACTION_VIEW => 'user/view',
            ],
        ];
    }
}

'urlManager' => [
    'class' => 'voskobovich\seo\web\UrlManager',
    'cacheable' => false,
    'rules' => [
        '' => 'post/index',

        ['class' => '\voskobovich\seo\web\ClearUrlRule'],
        ['class' => '\voskobovich\seo\web\UrlRule', 'modelClass' => 'app\models\UrlRoute'],

        // Default
        '<controller:\w+>/<id:\d+>' => '<controller>/view',
        '<controller:\w+>/<action:[a-zA-Z-]*>/<id:\d+>' => '<controller>/<action>',
        '<controller:\w+>/<action:[a-zA-Z-]*>' => '<controller>/<action>',
    ]
],

class Post extends BaseActiveRecord implements SeoModelInterface
{
    // ...

    /**
     * @return array
     */
    public function behaviors()
    {
        return [
            'createUrlBehavior' => [
                'class' => CreateUrlBehavior::className(),
                'modelClass' => UrlRoute::className(),
                'objectKey' => UrlRoute::OBJECT_POST
            ],
            'actualityUrlBehavior' => [
                'class' => ActualityUrlBehavior::className(),
                'modelClass' => UrlRoute::className(),
                'objectKey' => UrlRoute::OBJECT_POST
            ]
        ];
    }

    /**
     * Build Seo Path
     * @return null|string
     */
    public function getSeoPath()
    {
        /** @var Category|TreeInterface $mainCategory */
        $mainCategory = $this->mainCategory;
        if ($mainCategory) {
            return $mainCategory->path . '/' . $this->slug;
        }

        return null;
    }
    
    // ...
}

class PostController extends Controller
{
    // ...

    public function actionView($id)
    {
        /** @var Post $model */
        $model = Post::find()
            ->andWhere(['id' => $id])
            ->one();

        $model->trigger(ActualityUrlBehavior::EVENT_CHECK_URL);

        return $this->render('view', [
            'model' => $model
        ]);
    }
    
    // ...
}

 $form = ActiveForm::begin() 

 $form = ActiveForm::begin() 

<?= GridView::widget([
  'dataProvider' => $dataProvider,
  'filterModel' => $model,
  'columns' => [
      [
          'attribute' => 'path',
          'format' => 'raw',
          'value' => function ($model) {
              /** @var UrlRoute $model */
              return Html::a($model->path, ['update', 'id' => $model->id], ['data-pjax' => 0]);
          }
      ],
      [
          'attribute' => 'object_key',
          'filter' => $model::getObjectItems(),
          'value' => function ($model) {
              /** @var UrlRoute $model */
              return $model->getObject();
          },
          'visible' => array_search($action, [UrlRoute::ACTION_INDEX, UrlRoute::ACTION_VIEW]) !== false
      ],
      [
          'attribute' => 'object_id',
          'visible' => array_search($action, [UrlRoute::ACTION_INDEX, UrlRoute::ACTION_VIEW]) !== false
      ],
      [
          'attribute' => 'url_to',
          'visible' => $action == UrlRoute::ACTION_REDIRECT
      ],
      [
          'attribute' => 'http_code',
          'visible' => $action == UrlRoute::ACTION_REDIRECT
      ],
      [
          'class' => 'voskobovich\grid\advanced\columns\ActionColumn',
          'template' => '{view} {update} {delete}',
          'options' => [
              'width' => '160px'
          ],
          'buttons' => [
              'view' => function ($url, $model, $key) {
                  $options = [
                      'title' => Yii::t('yii', 'View'),
                      'aria-label' => Yii::t('yii', 'View'),
                      'data-pjax' => '0',
                      'class' => 'btn btn-default btn-xs',
                      'target' => '_blank',
                  ];
                  /** @var UrlRoute $model */
                  $url = $model->viewUrl();
                  return Html::a(Yii::t('yii', 'View'), $url, $options);
              }
          ]
      ],
  ],
]) 
bash
php yii migrate/create create_table__url_route
bash
php yii migrate

php composer.phar