sbs / yii2-seo

该模块允许将链接SEO字段关联到模型。

安装: 0

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 5

公开问题: 0

类型:yii2-extension

1.0.0 2020-04-23 09:47 UTC

This package is auto-updated.

Last update: 2024-09-23 18:57:43 UTC


README

该模块提供向模型添加SEO字段的功能。字段:标题、关键词、描述。

安装

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

运行以下命令

composer require sbs/yii2-seo

或添加到应用程序的 composer.json 文件中的 require 部分,然后运行

"sbs/yii2-seo": "^1.0"

composer update

迁移

要向数据库添加表,可以运行以下命令

php yii migrate/up --migrationPath=vendor/sbs/yii2-seo/src/migrations

或者您也可以配置应用程序的 config\console.php

此方法更可取,因为您可以使用所有标准迁移命令。

'controllerMap' => [
    'migrate' => [
        'class' => MigrateController::class,
        'migrationPath' => [
            '@console/migrations',
            '@vendor/sbs/yii2-seo/src/migrations',
        ],
        //...
    ],
    //...
]

与模型一起使用

您需要向模型添加行为

use sbs\behaviors\SeoBehavior;

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

现在所有字段都将通过 $model->seo 可用。

在模型表单视图中添加/编辑字段,请使用以下小部件

//...
use sbs\widgets\SeoForm;
//...
?>
<div>
    <?php $form = ActiveForm::begin(); ?>
    //...
    <?= SeoForm::widget(['model' => $model, 'form' => $form]); ?>
    //...
    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create': 'Update'); ?>
    </div>
    <?php ActiveForm::end(); ?>
</div>

在页面视图中显示此字段,请使用以下小部件

use sbs\widgets\SeoTags;
//...
SeoTags::widget(['seo' => $model->seo]);
// ...

不使用模型

此外,您还可以在无需模型和数据库的情况下使用SeoTags小部件。例如,如果您需要向某些静态页面添加SEO,并且不需要在管理面板中编辑此信息

use sbs\widgets\SeoTags;
//...
SeoTags::widget(['title' => 'title', 'keywords' => 'keyword 1, keyword 2', 'description' => 'your description']);
// ...