1. Go to this page and download the library: Download zhelyabuzhsky/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/ */
use yii\db\ActiveRecord;
use zhelyabuzhsky\sitemap\models\SitemapEntityInterface;
class Category extends ActiveRecord implements SitemapEntityInterface
{
/**
* @inheritdoc
*/
public function getSitemapLastmod()
{
return date('c');
}
/**
* @inheritdoc
*/
public function getSitemapChangefreq()
{
return 'daily';
}
/**
* @inheritdoc
*/
public function getSitemapPriority()
{
return 0.5;
}
/**
* @inheritdoc
*/
public function getSitemapLoc()
{
// Use urlManager rules to create urls
return $url = Yii::$app->urlManager->createAbsoluteUrl([
'page/view',
'pageSlug' => $this->slug,
]);
// or directly
// return 'http://localhost/' . $this->slug;
}
/**
* @inheritdoc
*/
public static function getSitemapDataSource()
{
return self::find();
}
}
use yii\console\Controller;
class SitemapController extends Controller
{
public function actionCreate()
{
\Yii::$app->sitemap
->addModel(Item::className())
->addModel(Category::className(), \Yii::$app->db) // Also you can pass \yii\db\Connection to the database connection that you need to use
->setDisallowUrls([
'#url1#',
'#url2$#',
])
->create();
}
}
bash
$ php composer.phar
bash
php yii sitemap/create
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.