pistol88 / yii2-seo
模块允许将SEO字段绑定到模型
dev-master
2017-04-08 06:00 UTC
Requires
- php: >=5.4.0
- yiisoft/yii2: *
- yiisoft/yii2-bootstrap: *
This package is auto-updated.
Last update: 2024-08-27 21:32:12 UTC
README
模块提供快速分配和释放SEO字段(如标题、描述、关键词等)的功能。
安装
执行命令
php composer require pistol88/yii2-seo "*"
或者添加到 composer.json
"pistol88/yii2-seo": "*",
然后执行
php composer update
迁移
php yii migrate --migrationPath=vendor/pistol88/yii2-seo/migrations
使用
将行为附加到模型
function behaviors() { return [ 'seo' => [ 'class' => 'pistol88\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字段输入
<?=\pistol88\seo\widgets\SeoForm::widget([
'model' => $model,
'form' => $form,
]); ?>
需要在模型的编辑表单内部调用它。