kovenant/yii2-seo-components Seo components for Yii 2

yii2-seo-components

  1. Installation
  2. SeoModelBehavior Usage
  3. MetaTagsWidget.php Usage

Latest Version Software License Build Status Scrutinizer Code Quality codecov

Installation

The preferred way to install this extension is via composer.

Either run

php composer.phar require --prefer-dist kovenant/yii2-seo-components "*"

or add this code line to the require section of your composer.json file:

"kovenant/yii2-seo-components": "*"

SeoModelBehavior Usage

Add SeoModelBehavior to your model

{id}, {category_id} and {alias} will be replaced by the value of the attributes of the current model, that have such names

{category.alias} will be replaced by the value of the relation’s attribute

    /**
     * @return array
     */
    public function behaviors()
    {
        return [
            [
                'class' => \kovenant\seo\SeoModelBehavior::class,
                'route' => ['catalog/item', 'categoryId' => '{category_id}', 'categoryAlias' => '{category.alias}', 'id' => '{id}', 'alias' => '{alias}']
            ],
        ];
    }

Behavior provides three methods:

  • $model->getRouteUrl() will return route as array. Now you can use it for yii\widgets\Menu items 'url'
Array
(
    [0] => catalog/item
    [categoryId] => 1
    [categoryAlias] => category-alias
    [id] => 5
    [alias] => item-alias
)
  • $model->getUrl() will return for example /catalog/1-category-alias/5-item-alias.html

  • $model->getAbsoluteUrl() returns https://example.com/catalog/1-category-alias/5-item-alias.html

Don't forget to check your config file for pretty url settings

E.g.

    'components' => [
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'suffix' => '.html',
            'rules' => [
                'catalog/<categoryId:\d+>-<categoryAlias>/<id:\d+>-<alias>' => 'catalog/item',
            ],
        ],
    ],

MetaTagsWidget.php Usage

Add MetaTagsWidget to your view

/** yii\db\ActiveRecord $model */
\kovenant\seo\MetaTagsWidget::widget(['component' => $model]);

In addition to the widget settings you can configure common widget options via container definitions in your config file

Example of full options:

    'container' => [
        'definitions' => [
            'kovenant\seo\MetaTagsWidget' => [
                // set view attributes
                'viewH1Attribute' => 'h1', /* use <h1><?= $this->h1 ?></h1> in your view/layout */
                'viewTitleAttribute' => 'title', /* will produce <title> */

                // Set the model attributes
                'componentNameAttribute' => 'name', // default name attribute (e.g. for link name)
                'componentH1Attribute' => 'h1', // h1 for page
                'componentTitleAttribute' => 'title', // meta title
                'componentKeywordsAttribute' => 'keywords', // meta keywords
                'componentDescriptionAttribute' => 'description', // meta description

                // set pager params
                'pageText' => 'Страница', // page text [Page for default]
                'pageParam' => 'page', // get param for current page

                // In all templates placeholder {text} is an original value
                'templateH1' => '{text}',
                'templateTitle' => '{text}{pager} | {appName}', // {appName} will add name of application
                'templateKeywords' => '{text}',
                'templateDescription' => '{text}{pager}', // {pager} will be replaced with text about current page
                'templatePager' => ' - {pageText} {pageValue}', // template for such replacement

                // method from \kovenant\seo\SeoModelBehavior that will return absolute url for the page of this record
                'absoluteUrlMethod' => 'getAbsoluteUrl'
            ]
        ],
    ],
    'view' => [
        //you can use custom view for h1 support
        'class' => 'kovenant\seo\SeoView',
    ],

componentNameAttribute is required. Other component attributes are optional.

The text for default value of title and description is componentNameAttribute.

If viewH1Attribute and componentH1Attribute are set they will be used as a default value.

componentTitleAttribute, componentKeywordsAttribute and componentDescriptionAttribute will set corresponding meta tags.

i.e. default value = componentNameAttribute << componentH1Attribute << componentTitleAttribute

If you set absoluteUrlMethod from SeoModelBehavior and current page of widget != absolute url from model, canonical link tag will be added.

You can inherit from MetaTagsWidget and add your own getters to use in templates, like {appName} and {pager}

See more examples of usage in tests.

1 0
1 follower
85 downloads
Yii Version: 2.0
License: MIT
Category: Web Service
Developed by: kovenant
Created on: Aug 1, 2020
Last updated: 5 months ago
Packagist Profile
Github Repository

Related Extensions