PHP code example of zhelyabuzhsky / yii2-sitemap

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/ */

    

zhelyabuzhsky / yii2-sitemap example snippets


'urlManager' => [
    'hostInfo' => 'https://example.com',
    'baseUrl' => '/',
    'rules' => [
      // ...
    ],
],

$frontendUrlManager =  array_merge($frontendUrlManager, [
    'hostInfo' => 'https://example.com'
]),


return [
    'baseUrl' => '/',
    'class' => 'yii\web\UrlManager',
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
      //...
    ],
];


'components' => [
  'sitemap' => [
    'class' => '\zhelyabuzhsky\sitemap\components\Sitemap',
  ],
],

'components' => [
  'sitemap' => [
    'class' => '\zhelyabuzhsky\sitemap\components\Sitemap',
    'maxUrlsCountInFile' => 10000,
    'sitemapDirectory' => 'frontend/web',
    'optionalAttributes' => ['changefreq', 'lastmod', 'priority'],
    'maxFileSize' => '10M',
  ],
 ],

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