odanylevskyi/yii2-sitemap-xml

Sitemap extension provides functionality to generate and send xml file to the search engines.

Installs: 185

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:yii2-extension

dev-master 2018-04-01 18:53 UTC

This package is not auto-updated.

Last update: 2024-09-23 15:25:14 UTC


README

Sitemap extension provides functionality to generate and send xml file to the search engines.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist odanylevskyi/yii2-sitemap-xml "@dev"

or add

"odanylevskyi/yii2-sitemap-xml": "@dev"

to the require section of your composer.json file.

Usage

Once the extension is installed, simply place the following code in your config\main.php in modules section:

'modules' => [
...
    'sitemap' => [
        'class' => '\odanylevskyi\sitemap\Module',
        'items' => [
            [
                'urls' => [
                    'site/index',
                    'site/login',
                    'site/contact',
                    ['hotel/view', 'id' => 1],
                    ....
                ],
            ],
        ],
...
],

Also you need to add this line to the urlManager in components section 'sitemap.xml' => 'sitemap/default/index':

[
    ...
    'rules' => [
        ...
        'sitemap.xml' => 'sitemap/default/index',
        ...
    ]
...

If you have more then one sitemap.xml file or you want to use sitemap-index.xml file you can add useIndex to the module settings:

...
    'sitemap' => [
           'class' => '\odanylevskyi\sitemap\Module',
           'useIndex' => true,
           ...
   ],
...

To build url using models you need to add the following to the module configuration:

...
    'sitemap' => [
       'class' => '\odanylevskyi\sitemap\Module',
       'items' => [
           [
               'class' => 'frontend\models\Artile',
               'urls' => [
                   ['article/view', 'id' => ':id'],
                   ['article/view-by-name', 'name' => ':title'],
                   ....
               ],
           ],
       ],
...

where :id, :title should be valid attributes of Article model. Also You can add SQL rules to your model (e.g. Article). For example ,lets imagine that you want to add only articles that was accepted by moderator. You can do it in the following way:

...
    'sitemap' => [
       'class' => '\odanylevskyi\sitemap\Module',
       'items' => [
           [
               'class' => 'frontend\models\Artile', 
               'rules' => function($model) {
                   return $model->andWhere(['is_active'=>1]);
               },
               'urls' => [
                   ['article/view', 'id' => ':id'],
                   ['article/view-by-name', 'name' => ':title'],
                   ....
               ],
           ],
       ],
...

rules must be Closure instance another way it will be ignored.

To use file cache you need to add expire to the module settings where expire is a time in seconds. Default is -1 that means no caching.

...
    'sitemap' => [
       'class' => '\odanylevskyi\sitemap\Module',
       'expire' => 30*24*3600; //30 days from now
       ...
...

To specify priority and frequency for url use the next structure:

...
    'sitemap' => [
       'class' => '\odanylevskyi\sitemap\Module',
       'items' => [
           [
               'class' => 'frontend\models\Artile',
               'urls' => [
                   [
                      'path' => article/view', 'id' => ':id'],
                      'priority' => 0.5,
                      'freq' => 'monthly',
                   ],
                   [
                      'path' => 'article/view-by-name', 'name' => ':title'],
                      'priority' => 0.8,
                      'freq' => 'daily',
                   ],
                   ....
               ],
           ],
       ],
...