Yii 2.0: yii2-sitemap-module

Yii2 module for automatically generating XML Sitemap
8 followers

Yii2 module for automatically generating XML Sitemap.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist "himiklab/yii2-sitemap-module" "*"

or add

"himiklab/yii2-sitemap-module" : "*"

to the require section of your application's composer.json file.

Usage

  • Configure the cache component of your application's configuration file, for example:
'components' => [
    'cache' => [
        'class' => 'yii\caching\FileCache',
    ],
]
  • Add a new module in modules section of your application's configuration file, for example:
'modules' => [
    'sitemap' => [
        'class' => 'himiklab\sitemap\Sitemap',
        'models' => [
            // your models
            'app\modules\news\models\News',
        ],
        'urls'=> [
            // your additional urls
            [
                'loc' => '/news/index',
                'changefreq' => \himiklab\sitemap\behaviors\SitemapBehavior::CHANGEFREQ_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
    ],
],
  • Add behavior in the AR models, for example:
use himiklab\sitemap\behaviors\SitemapBehavior;
 
public function behaviors()
{
    return [
        'sitemap' => [
            'class' => SitemapBehavior::className(),
            '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' => SitemapBehavior::CHANGEFREQ_DAILY,
                    'priority' => 0.8
                ];
            }
        ],
    ];
}
  • Add a new rule for urlManager of your application's configuration file, for example:
'urlManager' => [
    'rules' => [
        ['pattern' => 'sitemap', 'route' => 'sitemap/default/index', 'suffix' => '.xml'],
    ],
],

Resources

Total 5 comments

#19310 report it
Ankita Ppuria at 2015/05/18 07:46am
I am getting an error when opening the xml in browser

I am getting this error when I opened the xml .The xml was generated in vendor folder.I removed the below error by removing extra space. But still in xml the {{}} value doesnot get replaced it shows as is in browser.

This page contains the following errors: error on line 1 at column 1: Document is empty Below is a rendering of the page up to the first error.

#18253 report it
himiklab at 2014/10/07 11:50am
Typos in docs

Thanks! I apologize for embarrassing typos, I will fix it.

#18252 report it
oligalma at 2014/10/07 11:36am
Two errors

There are two errors in your behaviors method, which I solved.

1) It says:

Url::to(model->url, true)

and should be:

Url::to($model->url, true)

2) It says:

use himiklab\sitemap\SitemapBehavior;

and should be:

use himiklab\sitemap\behaviors\SitemapBehavior;
#18249 report it
himiklab at 2014/10/07 09:25am
Solution

Replace SitemapBehavior in main.php to \himiklab\sitemap\behaviors\SitemapBehavior.

#18243 report it
oligalma at 2014/10/06 05:02pm
I get an error

I installed your extension via composer and followed your instructions but I get this error:

Fatal error: Class 'SitemapBehavior' not found in C:\xampp\htdocs\advanced\common\config\main.php on line 19

Leave a comment

Please to leave your comment.

Create extension
Downloads
No downloadable files yet