PHP code example of leama / sitemap-generator

1. Go to this page and download the library: Download leama/sitemap-generator 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/ */

    

leama / sitemap-generator example snippets


use App\Application\SitemapGeneration\Enum\FormatEnum;
use App\Application\SitemapGeneration\Exception\FormatException;
use App\Infrastructure\SitemapGenerator\Exception\SitemapException;
use App\Infrastructure\SitemapGenerator\SitemapGenerator;

$pages = [
    [
        'loc' => 'https://site.com/',
        'lastmod' => '2024-07-23',
        'priority' => 1.0,
        'changefreq' => 'daily'
    ],
];

try {
    $sitemapGenerator = new SitemapGenerator($pages, SitemapGenerationFormat::CSV, 'path/to/file.csv');
    $sitemapGenerator->generate();

    echo 'File was generate successfully!';

} catch (FormatException|SitemapException $e) {
    echo "Ошибка: " . $e->getMessage();
}

use App\Application\SitemapGeneration\Enum\FormatEnum;
use App\Application\SitemapGeneration\Exception\FormatException;
use App\Infrastructure\SitemapGenerator\Exception\SitemapException;
use App\Infrastructure\SitemapGenerator\SitemapGenerator;

$pages = [
    [
        'loc' => 'site', // Неверный формат
        'lastmod' => '2024-06-29',
        'priority' => 1.0,
        'changefreq' => 'tomorrow'
    ]
];

// Инициализация...

use App\Application\SitemapGeneration\Enum\FormatEnum;
use App\Application\SitemapGeneration\Exception\FormatException;
use App\Infrastructure\SitemapGenerator\Exception\SitemapException;
use App\Infrastructure\SitemapGenerator\SitemapGenerator;

$pages = [
    [
        'loc' => 'https://site.com/',
        'lastmod' => '23.07.2024', // Неверный формат
        'priority' => 1.0,
        'changefreq' => 'tomorrow'
    ]
];

// Инициализация...

use App\Application\SitemapGeneration\Enum\FormatEnum;
use App\Application\SitemapGeneration\Exception\FormatException;
use App\Infrastructure\SitemapGenerator\Exception\SitemapException;
use App\Infrastructure\SitemapGenerator\SitemapGenerator;

$pages = [
    [
        'loc' => 'https://site.com/',
        'lastmod' => '2024-07-23',
        'priority' => 1.2, // Ошибка: priority должен быть от 0.0 до 1.0
        'changefreq' => 'daily'
    ]
];

// Инициализация...

use App\Application\SitemapGeneration\Enum\FormatEnum;
use App\Application\SitemapGeneration\Exception\FormatException;
use App\Infrastructure\SitemapGenerator\Exception\SitemapException;
use App\Infrastructure\SitemapGenerator\SitemapGenerator;

$pages = [
    [
        'loc' => 'https://site.com/',
        'lastmod' => '2024-07-23',
        'priority' => 1.0,
        'changefreq' => 'tomorrow' // Ошибка
    ]
];

// Инициализация...

use App\Application\SitemapGeneration\Enum\FormatEnum;
use App\Application\SitemapGeneration\Exception\FormatException;
use App\Infrastructure\SitemapGenerator\Exception\SitemapException;
use App\Infrastructure\SitemapGenerator\SitemapGenerator;

$pages = [];

try {
    $sitemapGenerator = new SitemapGenerator($pages, SitemapGenerationFormat::CSV, 'path/to/file.xml');
    $sitemapGenerator->generate();

    print 'File was generate successfully!';

} catch (FormatException|SitemapException $e) {
    print $e->getMessage();
}