drtsb/yii2-seo

Yii2 SEO模块。

安装: 87

依赖项: 0

建议者: 0

安全性: 0

星标: 2

关注者: 2

分支: 0

开放问题: 1

类型:yii2-extension

0.4.1 2020-05-02 10:10 UTC

This package is auto-updated.

Last update: 2024-09-19 22:42:25 UTC


README

Yii2 SEO模块

Latest Stable Version Total Downloads License Build Status

安装

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

运行

composer require drtsb/yii2-seo:*

或添加

"drtsb/yii2-seo" : "*"

到您的应用 composer.json 文件的 require 部分。

用法

该扩展由两部分组成

  • 模块 提供了一个简单的 CRUD 来编辑由控制器和操作指定的页面的SEO参数。
  • 模型行为 为特定模型添加SEO字段。

首先,您需要应用迁移

yii migrate --migrationPath=@vendor/drtsb/yii2-seo/src/migrations

或使用 命名空间迁移 并将

'controllerMap' => [
    ...
    'migrate' => [
        'class' => 'yii\console\controllers\MigrateController',
        'migrationNamespaces' => [
            'drtsb\yii\seo\migrations',
        ],
    ],
    ...
],

添加到您的应用配置文件中。

模块配置

'modules' => [
	...
    'seo' => [
        'class' => 'drtsb\yii\seo\Module',
        'accessRoles' => ['admin'], // Default: null
        'pagination' => ['pageSize' => 10], // Default: false
    ],
    ...
],

之后,您可以通过访问 /seo/default 来管理SEO

您可以在 控制器操作 字段中使用 * 通配符。例如,对于页面 site/index 将使用以下顺序中的第一条合适的行

将行为添加到您的前端控制器

use drtsb\yii\seo\behaviors\SeoBehavior;

public function behaviors()
{
    return [
        SeoBehavior::class,
    ];
}

与模型一起工作

将行为添加到您的模型

public function behaviors()
{
    return [
        'seo' => [
            'class' => 'drtsb\yii\seo\behaviors\SeoModelBehavior',
        ],
    ];
}

添加到模型的表单

<?= $model->seoFields($form) ?>

在前端视图中注册元标签

use drtsb\yii\seo\widgets\MetaTagsWidget;

MetaTagsWidget::widget(['view' => $this, 'model' => $model]);

您还可以使用 dataClosure 参数根据模型属性生成SEO值

public function behaviors()
{
    return [
        'seo' => [
            'class' => 'drtsb\yii\seo\behaviors\SeoModelBehavior',
            'dataClosure' => function($model) {
                return [
                    'meta_title' => $model->title . ' Title',
                    'meta_description' => $model->title . ' Description',
                    'meta_keywords' => $model->title . ' Keywords',
                    'meta_noindex' => true,
                    'meta_nofollow' => true,
                    'rel_canonical' => 'site/index', // or 'http://some.site'
                    'dont_use_empty' => true, // Default value false
                ];
            },
        ],
    ];
}