yiier / yii2-seo
用于处理模型SEO参数的库
v0.1
2020-02-06 06:33 UTC
Requires
- yiisoft/yii2: ~2.0.0
This package is auto-updated.
Last update: 2024-09-06 17:31:08 UTC
README
用于处理模型SEO参数的库
安装
安装此扩展的首选方法是使用 composer。
运行以下命令之一
php composer.phar require --prefer-dist yiier/yii2-seo "*"
或
"yiier/yii2-seo": "*"
将以下内容添加到您的 composer.json
文件的 require 部分中。
配置
更改您的配置文件
<?php return [ 'components' => [ 'view' => [ 'as seo' => [ 'class' => 'yiier\seo\SeoViewBehavior', 'names' => [ 'keywords' => 'blog,forecho', 'author' => getenv('APP_NAME'), ], 'properties' => [ [ 'property' => ['title', 'og:title'], 'content' => function () { return ' tag1, tag2'; }, ], 'title1' => 'title' ], ] ] ] ];
在模型文件中添加 SEO 模型行为
<?php public function behaviors() { return [ 'seo' => [ 'class' => 'yiier\seo\SeoModelBehavior', 'names' => [ 'viewport' => function (self $model) { return $model->title . ', tag1, tag2'; }, 'keywords' => 'blog,forecho', 'author' => 'author', // model field ], 'properties' => [ [ 'property' => ['title', 'og:title'], 'content' => function (self $model) { return $model->title . ', tag1, tag2'; }, ], 'title1' => 'title', [ 'property' => 'description', 'content' => function (self $model) { return $model->title . ', tag1, tag2'; }, ], ], ], ]; }
模型 PHPdoc
/**
* @property \yiier\seo\SeoModelBehavior $seoBehavior
* @method \yiier\seo\SeoModelBehavior getSeoBehavior()
*/
更改 /frontend/views/layouts/main.php
<?php /* @var $this \yii\web\View|\yiier\seo\SeoViewBehavior */ ?> <head> <!-- Replace default <title> tag --> <title><?= Html::encode($this->title) ?></title> <!-- by this line: --> <?php $this->renderMetaTags(); ?> ... </head>
使用方法
在模型的 "view.php" 文件中
// set SEO:meta data for current page $this->setSeoData($model->getSeoBehavior());
或在控制器中
Yii::$app->view->setSeoData($model->getSeoBehavior());
在 '/frontend/config/main.php' 中的简单 URL 规则示例
'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [ '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>', '<module>/<controller:\w+>/<action:\w+>/<id:\d+>' => '<module>/<controller>/<action>', 'post/<action:(index|create|update|delete)>' => 'post/<action>', 'post/<title:[-\w]+>' => 'post/view', ], ],