ar2rsoft/sitemap

Provides support for site map creation

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 18

Type:yii2-extension

v2.0.0.0 2018-06-04 01:18 UTC

README

12951949

Site Map Extension for Yii 2


This extension provides support for site map and site map index files generating.

For license information check the LICENSE-file.

Latest Stable Version Total Downloads Build Status

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist yii2tech/sitemap

or add

"yii2tech/sitemap": "*"

to the require section of your composer.json.

Usage

This extension provides support for site map and site map index files generation. You can use [[\yii2tech\sitemap\File]] for site map file composition:

use yii2tech\sitemap\File;

$siteMapFile = new File();

$siteMapFile->writeUrl(['site/index'], ['priority' => '0.9']);
$siteMapFile->writeUrl(['site/about'], ['priority' => '0.8', 'changeFrequency' => File::CHECK_FREQUENCY_WEEKLY]);
$siteMapFile->writeUrl(['site/signup'], ['priority' => '0.7', 'lastModified' => '2015-05-07']);
$siteMapFile->writeUrl(['site/contact']);

$siteMapFile->close();

In case you put sitemap generation into console command, you will need to manually configure URL manager parameters for it. For example:

return [
    'id' => 'my-console-application',
    'components' => [
        'urlManager' => [
            'hostInfo' => 'http://example.com',
            'baseUrl' => '/',
            'scriptUrl' => '/index.php',
        ],
        // ...
    ],
    // ...
];

Creating site map index files

There is a limitation on the site map maximum size. Such file can not contain more then 50000 entries and its actual size can not exceed 10MB. If you web application has more then 50000 pages and you need to generate site map for it, you'll have to split it between several files and then generate a site map index file. It is up to you how you split your URLs between different site map files, however you can use [[\yii2tech\sitemap\File::getEntriesCount()]] or [[\yii2tech\sitemap\File::getIsEntriesLimitReached()]] method to check count of already written entries.

For example: assume we have an 'item' table, which holds several millions of records, each of which has a detail view page at web application. In this case generating site map files for such pages may look like following:

use yii2tech\sitemap\File;
use app\models\Item;

$query = Item::find()->select(['slug'])->asArray();

$siteMapFileCount = 0;
foreach ($query->each() as $row) {
    if (empty($siteMapFile)) {
        $siteMapFile = new File();
        $siteMapFileCount++;
        $siteMapFile->fileName = 'item_' . $siteMapFileCount . '.xml';
    }

    $siteMapFile->writeUrl(['item/view', 'slug' => $row['slug']]);
    if ($siteMapFile->getIsEntriesLimitReached()) {
        unset($siteMapFile);
    }
}

Once all site map files are generated, you can compose index file, using following code:

use yii2tech\sitemap\IndexFile;

$siteMapIndexFile = new IndexFile();
$siteMapIndexFile->writeUp();

Note: by default site map files are stored under the path '@app/web/sitemap'. If you need a different file path you should adjust [[fileBasePath]] field accordingly.