nox-it/yii2-nox-sitemaps

This package is abandoned and no longer maintained. The author suggests using the himiklab/yii2-sitemap-module package instead.

Yii2 XML Sitemap Generator

1.0.1 2016-05-30 14:27 UTC

This package is auto-updated.

Last update: 2022-02-01 12:58:40 UTC


README

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 "nox-it/yii2-nox-sitemaps" "*"

or add

"nox-it/yii2-nox-sitemaps" : "*"

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

  • 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' => [
    'sitemaps' => [
        'class' => 'nox\modules\sitemaps\Module',
        'models' => [
            // your models
            'app\modules\news\models\News',
            // or configuration for creating a behavior
            [
                'class' => 'app\modules\news\models\News',
                'behaviors' => [
                    '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
                            ];
                        }
                    ]
                ]
            ]
        ],
        'urls'=> [
            // your additional urls
            [
                'loc' => '/news/index',
                'changefreq' => \nox\modules\sitemaps\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 nox\modules\sitemaps\behaviors\SitemapBehavior;

public function behaviors()
{
    return [
        'sitemap' => [
            'class' => SitemapBehavior::className(),
            'scope' => function ($model) {
                /** @var \yii\db\ActiveQuery $model */
                $model->select(['url', 'lastmod']);
                $model->andWhere(['isDeleted' => 0]);
            },
            'dataClosure' => function ($model) {
                /** @var static $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' => 'sitemaps/default/index', 'suffix' => '.xml'],
    ],
],

Resources

Other Informations

This module was based on himiklab/yii2-sitemap-module.

Yii2