1. Go to this page and download the library: Download mrssoft/yii2-sitemap library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mrssoft / yii2-sitemap example snippets
namespace app\controllers;
use \mrssoft\sitemap\Sitemap;
class SitemapController extends \mrssoft\sitemap\SitemapController
{
/**
* @var int Cache duration, set null to disabled
*/
protected $cacheDuration = 43200; // default 12 hour
/**
* @var string Cache filename
*/
protected $cacheFilename = 'sitemap.xml';
protected $enablePriority = false;
protected $enableChangeFreq = false;
public function models(): array
{
return [
[
'class' => \app\models\Page::class,
'change' => Sitemap::MONTHLY,
'priority' => 0.8,
'lastmod' => 'updated_at',
]
];
}
public function urls(): array
{
return [
[
'url' => ['about/index'],
'priority' => 0.8
]
];
}
}
namespace app\models;
class Page extends \yii\db\ActiveRecord implements \mrssoft\sitemap\SitemapInterface
{
...
/**
* @return \yii\db\ActiveQuery
*/
public static function sitemap(): \yii\db\ActiveQuery
{
return self::find()->where(['public' => 1]);
}
/**
* @return string
*/
public function getSitemapUrl(): string
{
return \yii\helpers\Url::toRoute(['page/view', 'url' => $this->url], true);
}
}