PHP code example of yii2tech / sitemap

1. Go to this page and download the library: Download yii2tech/sitemap library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yii2tech / sitemap example snippets




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();



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



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

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

$siteMapFileCount = 0;
foreach ($query->each() as $row) {
    if (empty($siteMapFile)) {
        // if there is no active file - create one with unique name:
        $siteMapFile = new File();
        $siteMapFileCount++;
        $siteMapFile->fileName = 'item_' . $siteMapFileCount . '.xml';
    }

    $siteMapFile->writeUrl(['item/view', 'slug' => $row['slug']]);
    if ($siteMapFile->getIsEntriesLimitReached()) {
        // once file is full - close it, allowing creating a new one at the next cycle iteration:
        unset($siteMapFile);
    }
}



use yii2tech\sitemap\IndexFile;

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



return [
    'components' => [
        'urlManager' => [
            'rules' => [
                'sitemap.xml' => 'site/sitemap',
                // ...
            ],
        ],
        // ...
    ],
    // ...
];



namespace app\controllers;

use Yii;
use yii\web\Controller;
use yii\web\Response;
use yii2tech\sitemap\File;

class SiteController extends Controller
{
    public function actionSitemap()
    {
        // get content from cache:
        $content = Yii::$app->cache->get('sitemap.xml');
        if ($content === false) {
            // if no cached value exists - create an new one
            // create sitemap file in memory:
            $sitemap = new File();
            $sitemap->fileName = 'php://memory';
            
            // write your site URLs:
            $sitemap->writeUrl(['site/index'], ['priority' => '0.9']);
            // ...
            
            // get generated content:
            $content = $sitemap->getContent();

            // save generated content to cache
            Yii::$app->cache->set('sitemap.xml', $content);
        }

        // send sitemap content to the user agent:
        $response = Yii::$app->getResponse();
        $response->format = Response::FORMAT_RAW;
        $response->getHeaders()->add('Content-Type', 'application/xml;');
        $response->content = $content;
        
        return $response;
    }
}



use yii2tech\sitemap\File;

$siteMapFile = new File([
    'header' => '<?xml version="1.0" encoding="UTF-8"



use yii2tech\sitemap\File;

$siteMapFile = new File([
    'rootTag' => [
        'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9',
        'xmlns:image' => 'http://www.google.com/schemas/sitemap-image/1.1', // you will need to add XML namespace for non-standard tags
    ],
]);

$siteMapFile->writeUrl(['site/index'], [
    'images' => [
        [
            'url' => 'http://example.com/images/logo.jpg',
            'title' => 'Logo',
        ],
        [
            'url' => 'http://example.com/images/avatars/john-doe.jpg',
            'title' => 'Author',
        ],
        // ...
    ],
]);
// ...

$siteMapFile->close();



use yii2tech\sitemap\File;

$siteMapFile = new File([
    'rootTag' => [
        'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9',
        'xmlns:video' => 'http://www.google.com/schemas/sitemap-video/1.1', // you will need to add XML namespace for non-standard tags
    ],
]);

$siteMapFile->writeUrl(['site/index'], [
    'videos' => [
        [
            'title' => 'Demo video',
            'description' => 'Demo video of the main process',
            'thumbnailUrl' => 'http://example.com/images/demo-video.jpg',
            'player' => [
                'url' => 'http://example.com/videos/demo.flv',
                'allowEmbed' => true,
                'autoplay' => 'ap=1',
            ],
            'publicationDate' => '2019-08-02',
            'duration' => 240,
        ],
        [
            'title' => 'Our team',
            'description' => 'Greetings from our team',
            'thumbnailUrl' => 'http://example.com/images/our-team.jpg',
            'player' => [
                'url' => 'http://example.com/videos/our-team.flv',
                'allowEmbed' => true,
                'autoplay' => 'ap=1',
            ],
            'publicationDate' => '2019-08-02',
            'duration' => 120,
        ],
        // ...
    ],
]);
// ...

$siteMapFile->close();



use yii2tech\sitemap\File;

$siteMapFile = new File([
    'rootTag' => [
        'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9',
        'xmlns:image' => 'http://www.google.com/schemas/sitemap-image/1.1', // you will need to add XML namespace for non-standard tags
    ],
]);

$siteMapFile->writeUrl(
    ['site/index'],
    [],
    '<image:image><image:loc>http://example.com/images/logo.jpg</image:loc></image:image>'
);
// ...

$siteMapFile->close();

php composer.phar