prokhonenkov / yii2-sitemap-generator
The sitemap generator
Installs: 63
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- php: >=7.1
- samdark/sitemap: ^2.2
- yiisoft/yii2: ~2.0.0
This package is auto-updated.
Last update: 2024-10-25 17:14:30 UTC
README
This extension generates sitemap.xml file. The extension uses https://github.com/samdark/sitemap.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require prokhonenkov/yii2-sitemap-generator
or add
"prokhonenkov/yii2-sitemap-generator": "*"
to the require section of your composer.json
file.
Configuration
Add component declaration to your config file for web config:
<?php return [ // ... your config 'components' => [ 'sitemap' => [ 'class' => \prokhonenkov\sitemapgenerator\SitemapGenerator::class, // The class which implements SitemapSourceInterface, SitemapItemInterface 'baseUrl' => 'https://sitename.com', 'sitemapPath' => '@webroot', 'models' => [ \app\models\PostsSitemap::class, \app\models\ProductsSitemap::class, ], 'languages' => [ 'ru-RU', 'en-US', 'kk-KZ', ], ] ] ];
You need to create two classes. First class should to implements SitemapSourceInterface, SitemapItemInterface and second class should implement SitemapItemInterface:
<?php use app\modules\posts\models\Posts; use prokhonenkov\sitemapgenerator\interfaces\SitemapItemInterface; use prokhonenkov\sitemapgenerator\interfaces\SitemapSourceInterface; use samdark\sitemap\Sitemap; use yii\helpers\ArrayHelper; use yii\helpers\Url; class PostsSitemap extends Posts implements SitemapSourceInterface, SitemapItemInterface { public function getSitemapItems(): array { return ArrayHelper::merge([new PostsSitemapItem()], self::find()->all()); } public function getSitemapName(): string { return 'posts'; } public function getLastModified(): int { return strtotime($this->updated_at); } public function getLocation($index = null): string { return Url::to(['/posts/view', 'id' => $this->id], true); } public function getChangeFrequency(): string { return Sitemap::MONTHLY; } public function getPriority(): string { return 0.5; } }
<?php use app\modules\posts\models\Posts; use prokhonenkov\sitemapgenerator\interfaces\SitemapItemInterface; use samdark\sitemap\Sitemap; use yii\helpers\Url; class PostsSitemapItem extends Posts implements SitemapItemInterface { public function getLastModified(): int { return strtotime( Posts::find() ->max('updated_at')); } public function getLocation($language = null): string { \Yii::$app->language = $language; return Url::to(['/posts/index'], true); } public function getPriority(): string { return 1; } public function getChangeFrequency(): string { return Sitemap::DAILY; } }
Usage
Put this code in your ActiveRecord model in afterSave method:
public function afterSave($insert, $changedAttributes) { parent::afterSave($insert, $changedAttributes); \Yii::$app->sitemap->generate(); }
Eventually will be created a structure of files: