该模块允许将SEO字段绑定到模型

安装: 0

依赖项: 0

建议者: 0

安全性: 0

星标: 0

关注者: 0

分支: 5

类型:yii2-extension

dev-master 2022-04-29 13:52 UTC

This package is not auto-updated.

Last update: 2024-09-29 00:21:52 UTC


README

该模块允许快速分配和解包SEO字段:标题、描述、关键词(TDK)等。

安装

执行命令

php composer require liemuar/yii2-seo "*"

或者在composer.json中添加

"liemuar/yii2-seo": "*",

然后执行

php composer update

迁移

php yii migrate --migrationPath=vendor/liemuar/yii2-seo/migrations

使用

将行为附加到模型

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

现在所有SEO字段在调用 $model->seo 时都可用。

在视图文件中的使用示例

if(!$title = $model->seo->title) {
    $title = "Купить {$model->name} в Кургане в магазине «Шоп45»";
}

if(!$description = $model->seo->description) {
    $description = 'Страница '.$model->name;
}

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

$this->title = $title;

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

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

小部件

输入SEO字段

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

需要在模型编辑表单内部调用。