Skip to content
Site map creation support
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src code cleanup Jan 24, 2019
tests code cleanup Jan 24, 2019
.gitattributes Initial commit Jul 2, 2015
.gitignore Initial commit Jul 2, 2015
.travis.yml source code moved to 'src' directory Mar 2, 2018
CHANGELOG.md release 1.0.2 Jan 24, 2019
LICENSE.md Initial commit Jul 2, 2015
README.md console application config example added Mar 2, 2018
composer.json source code moved to 'src' directory Mar 2, 2018
phpunit.xml.dist source code moved to 'src' directory Mar 2, 2018

README.md

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.

You can’t perform that action at this time.