kovenant/yii2-seo-components

为 Yii 2 服务的 SEO 组件

安装: 156

依赖: 0

建议者: 0

安全: 0

星星: 2

关注者: 0

分支: 1

开放问题: 0

类型:yii2-extension

1.1.0 2020-08-12 16:39 UTC

This package is auto-updated.

Last update: 2024-09-14 02:22:57 UTC


README

Latest Version Software License Build Status Scrutinizer Code Quality codecov

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一

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

或将其代码行添加到您的 composer.json 文件的 require 部分

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

SeoModelBehavior 使用

将 SeoModelBehavior 添加到您的模型

{id}, {category_id} 和 {alias} 将被替换为具有相应名称的当前模型属性的值

{category.alias} 将被替换为关系的属性值

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

行为提供了三个方法

  • $model->getRouteUrl() 将返回数组形式的路由。现在您可以使用它为 yii\widgets\Menu 项目的 'url' 属性
Array
(
    [0] => catalog/item
    [categoryId] => 1
    [categoryAlias] => category-alias
    [id] => 5
    [alias] => item-alias
)
  • $model->getUrl() 将返回例如 /catalog/1-category-alias/5-item-alias.html

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

别忘了检查您的配置文件中的漂亮 URL 设置

例如。

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

MetaTagsWidget.php 使用

将 MetaTagsWidget 添加到您的视图中

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

除了小部件设置外,您还可以通过配置文件中的容器定义来配置常见的小部件选项

完整选项示例

    '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 是必需的。其他组件属性是可选的。

标题和描述的默认文本是 componentNameAttribute

如果设置了 viewH1AttributecomponentH1Attribute,则它们将被用作默认值。

componentTitleAttributecomponentKeywordsAttributecomponentDescriptionAttribute 将设置相应的元标签。

即默认值 = componentNameAttribute << componentH1Attribute << componentTitleAttribute

如果从 SeoModelBehavior 设置了 absoluteUrlMethod 且小部件的当前页面不是模型的绝对 URL,则将添加规范链接标签。

您可以从 MetaTagsWidget 继承并添加自己的获取器以在模板中使用,如 {appName} 和 {pager}

请参阅测试中的更多使用示例。