PHP code example of assayer-pro / yii2-sitemap-module

1. Go to this page and download the library: Download assayer-pro/yii2-sitemap-module 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/ */

    

assayer-pro / yii2-sitemap-module example snippets


'components' => [
    'cache' => [
        'class' => 'yii\caching\FileCache',
    ],
]

'modules' => [
    'sitemap' => [
        'class' => 'assayerpro\sitemap\Module',
    ],
...
],

'components' => [
    'robotsTxt' => [
        'class' => 'assayerpro\sitemap\RobotsTxt',
        'userAgent' => [
            // Disallow url for all bots
            '*' => [
                'Disallow' => [
                    ['/api/default/index'],
                ],
                'Allow' => [
                    ['/api/doc/index'],
                ],
            ],
            // Block a specific image from Google Images
            'Googlebot-Image' => [
                'Disallow' => [
                    // All images on your site from Google Images
                    '/',
                    // Files of a specific file type (for example, .gif)
                    '/*.gif$',
                ],
            ],
        ],
    ],
    'sitemap' => [
        'class' => 'assayerpro\sitemap\Sitemap',
        'models' => [
            // your models
            'app\modules\news\models\News',
            // or configuration for creating a behavior
            [
                'class' => 'app\modules\news\models\News',
                'behaviors' => [
                    'sitemap' => [
                        'class' => '\assayerpro\sitemap\behaviors\SitemapBehavior',
                        'scope' => function ($model) {
                            /** @var \yii\db\ActiveQuery $model */
                            $model->select(['url', 'lastmod']);
                            $model->andWhere(['is_deleted' => 0]);
                        },
                        'dataClosure' => function ($model) {
                            /** @var self $model */
                            return [
                                'loc' => Url::to($model->url, true),
                                'lastmod' => strtotime($model->lastmod),
                                'changefreq' => \assayerpro\sitemap\Sitemap::DAILY,
                                'priority' => 0.8
                            ];
                        }
                    ],
                ],
            ],
        ],
        'urls'=> [
            // your additional urls
            [
                'loc' => ['/news/default/index'],
                'changefreq' => \assayerpro\sitemap\Sitemap::DAILY,
                'priority' => 0.8,
                'news' => [
                    'publication'   => [
                        'name'          => 'Example Blog',
                        'language'      => 'en',
                    ],
                    'access'            => 'Subscription',
                    'genres'            => 'Blog, UserGenerated',
                    'publication_date'  => 'YYYY-MM-DDThh:mm:ssTZD',
                    'title'             => 'Example Title',
                    'keywords'          => 'example, keywords, comma-separated',
                    'stock_tickers'     => 'NASDAQ:A, NASDAQ:B',
                ],
                'images' => [
                    [
                        'loc'           => 'http://example.com/image.jpg',
                        'caption'       => 'This is an example of a caption of an image',
                        'geo_location'  => 'City, State',
                        'title'         => 'Example image',
                        'license'       => 'http://example.com/license',
                    ],
                ],
            ],
        ],
        'enableGzip' => true, // default is false
        'cacheExpire' => 1, // 1 second. Default is 24 hours,
        'sortByPriority' => true, // default is false
    ],
],

use assayerpro\sitemap\behaviors\SitemapBehavior;

public function behaviors()
{
    return [
        'sitemap' => [
            'class' => SitemapBehavior::className(),
            /**'batchSize' => 100,*/
            'scope' => function ($model) {
                /** @var \yii\db\ActiveQuery $model */
                $model->select(['url', 'lastmod']);
                $model->andWhere(['is_deleted' => 0]);
            },
            'dataClosure' => function ($model) {
                /** @var self $model */
                return [
                    'loc' => Url::to($model->url, true),
                    'lastmod' => strtotime($model->lastmod),
                    'changefreq' => Sitemap::DAILY,
                    'priority' => 0.8
                ];
            }
        ],
    ];
}

'urlManager' => [
    'rules' => [
        ['pattern' => 'sitemap-<id:\d+>', 'route' => '/sitemap/default/index', 'suffix' => '.xml'],
        ['pattern' => 'sitemap', 'route' => 'sitemap/default/index', 'suffix' => '.xml'],
    ],
],

    'controllerMap' => [
        'sitemap' => [
            'class' => 'assayerpro\sitemap\console\CreateController',
        ],
    ],

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

php composer.phar