2017 © Pedro Peláez
 

yii2-extension yii2-seomodule

SEO-oriented functional for models and controllers

image

nevmerzhitsky/yii2-seomodule

SEO-oriented functional for models and controllers

  • Wednesday, January 20, 2016
  • by nevmerzhitsky
  • Repository
  • 2 Watchers
  • 0 Stars
  • 19 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

yii2-seomodule

Add ability to edit content of SEO-oriented HTML tags and attributes. Also add ability to configure redirection from any route to another with 301 status. And more SEO-oriented functions., (*1)

Highly inspired by https://github.com/demisang/yii2-seo and https://github.com/Amirax/yii2-seo-tools., (*2)

Installation

The preferred way to install this extension is through composer., (*3)

Either run, (*4)

php composer.phar require nevmerzhitsky/yii2-seomodule "*"

or add, (*5)

"nevmerzhitsky/yii2-seomodule": "*"

to the require section of your composer.json file. Then run command, (*6)

php composer.phar update

After installation extension run migration:, (*7)

./yii migrate --migrationPath="@vendor/nevmerzhitsky/yii2-seomodule/migrations"

Configuration of demisang/yii2-seo

In components configuration add the following:, (*8)

[
    'components' => [
        'seo' => [
            'class' => 'nevmerzhitsky\seomodule\Meta'
        ],
        'view' => [
            'as seo' => [
                'class' => 'nevmerzhitsky\seomodule\SeoViewBehavior',
            ]
        ],
    ],
    ...
];

And add SEO extension to bootstrap:, (*9)

'bootstrap' => ['log', 'seo']

In model file add seo model behavior:, (*10)

public function behaviors()
{
    $it = $this;

    return [
        'seo' => [
            'class' => 'nevmerzhitsky\seomodule\SeoModelBehavior',
            'title' => [
                'produceFunc' => 'title',
                'produceMaxLength' => 150,
                'overrideByDb' => false
            ],
            'descriptionProduceFunc' => 'short_desc',
            'keysProduceFunc' => function ($model) {
                /* @var $model self|\yii\db\ActiveRecord */
                return $model->title . ', tag1, tag2';
            },
            'metaField' => 'seo_meta',
            'userCanEdit' => Yii::$app->has('user') && Yii::$app->user->can(User::ROLE_ADMIN),
            // 'languages' => 'ru',
            'urlField' => 'seo_url',
            'urlProduceField' => 'title',
            'controllerClassName' => '\frontend\controllers\PostController',
            'uniqueUrlFilter' => function ($query) use ($it) {
                /* @var $query \yii\db\Query */
                $query->andWhere(['category_id' => $it->category_id]);
            },
        ],
    ];
}

In main layout:, (*11)

<head>
    <title><?php echo Html::encode(Yii::$app->seo->title); ?></title>
<?php $this->head(); ?>
    ...
</head>

Usage

In a controller action for the model ("view" for example):, (*12)

Yii::$app->seo->registerModel($model);

You can register several models in one action. If these models have 'seo' behavior, then SEO data will combined from all of them in registration order., (*13)

In admin/manager site you can add fields for editing SEO data of a model ("_form.php" template):, (*14)

<?php
$this->beginContent('@app/vendor/nevmerzhitsky/yii2-seomodule/views/edit-form.php',
    [
        'model' => $model,
        'form' => $form
    ]);
$this->endContent();
?>

Configuration and using of the Amirax/yii2-seo-tools

SEO Meta

Extension will automatically load the correct row from the database using the currently running and params.You can optionally override data by specifying them in a parameter array, (*15)

Yii::$app->seo->title = 'Page title';
Yii::$app->seo->metakeys = 'seo,yii2,extension';
Yii::$app->seo->metadesc = 'Page meta description';
Yii::$app->seo->tags['og:type'] = 'article';

You can set the templates for tags. For example:, (*16)

Yii::$app->seo->setVar('USER_NAME', 'Smith');
Yii::$app->seo->tags['og:title'] = 'Hello %USER_NAME%';

Default variables: * %HOME_URL% - Homepage url * %CANONICAL_URL% - Canonical URL for current page * %LOCALE% - Site locale, (*17)

SEO Redirect

For enabling SEO Redirect add to configuration file, (*18)

'errorHandler' => [
    'class' => 'nevmerzhitsky\seomodule\Redirect',
],

The Versions

20/01 2016

dev-master

9999999-dev https://github.com/nevmerzhitsky/yii2-seomodule#readme

SEO-oriented functional for models and controllers

  Sources   Download

MIT

The Requires

 

extension yii2 seo