此包最新版本(dev-master)没有可用的许可证信息。

Yii2 SEO 模块

dev-master 2021-10-15 08:37 UTC

This package is auto-updated.

Last update: 2024-09-15 14:49:53 UTC


README

为您的模型添加 SEO 字段:标题、描述、关键词等

安装

运行

php composer require pantera-digital/yii2-seo "*"

或将以下内容添加到 composer.json 中

"pantera-digital/yii2-seo": "*",

并执行

php composer update

迁移

php yii migrate --migrationPath=vendor/pantera-digital/yii2-seo/migrations

配置

该模块依赖于 https://github.com/yiisoft/yii2-twig,要完全使用预设,需要配置此扩展

用法

将行为附加到您的模型

    function behaviors()
    {
        return [
            'seo' => [
                'class' => 'pantera\seo\behaviors\SeoFields',
            ],
        ];
    }

在视图中使用示例

if (!$title = $model->seo->title) {
    $title = "Buy {$model->name} in store";
}

if (!$description = $model->seo->description) {
    $description = 'Page ' . $model->name;
}

if (!$keywords = $model->seo->keywords) {
    $keywords = '';
}

$this->title = $title;

$this->registerMetaTag([
    'name' => 'description',
    'content' => $description,
]);

$this->registerMetaTag([
    'name' => 'keywords',
    'content' => $keywords,
]);

小部件

添加到您的模型表单

<?=\pantera\seo\widgets\SeoForm::widget([
        'model' => $model, 
        'form' => $form, 
    ]); ?>

需要在配置中连接组件

'components' => [
    'seo' => [
        'class' => pantera\seo\components\SeoComponent::className(),
    ],
]

按地址使用元数据注册

需要在应用程序的 Bootstrap 类中连接 Bootstrap 应用程序类

'bootstrap' => [pantera\seo\Registrar::className()],

未找到

为记录日志需要在错误处理程序中添加

if ($exception instanceof NotFoundHttpException) {
    $logger = new pantera\seo\models\SeoNotFound();
    $logger->url = Yii::$app->request->url;
    $logger->referrer = Yii::$app->request->referrer;
    $logger->ip = Yii::$app->request->getRemoteIP();
    $logger->save();
}

短名

在模型中添加行为

public function behaviors()
{
    return [
        [
            'class' => pantera\seo\behaviors\SlugBehavior::className(),
            'attribute' => 'title',
            'slugAttribute' => 'slug',
        ],
    ];
}

在模型中连接验证器

public function rules()
{
    return [
        [['slug'], pantera\seo\validators\SlugValidator::className(), 'skipOnEmpty' => false],
    ];
}

配置自己的 UrlManager

模块具有注册 Open Graph 标签的能力

需要配置扩展 https://packagist.org.cn/packages/dragonjet/yii2-opengraph

'components' => [
    'openGraph' => [
        'class' => dragonjet\opengraph\OpenGraph::class,
    ],
],

在 Og Image 字段中需要写入如何使用 twig 获取图片