prokhonenkov/yii2-sitemap-generator

The sitemap generator

1.3 2019-09-25 05:54 UTC

This package is auto-updated.

Last update: 2024-04-25 15:57:53 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:

screnshot