PHPackages                             cahuk/sitemap-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. cahuk/sitemap-generator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

cahuk/sitemap-generator
=======================

Flexible PHP sitemap generator

1.0.0(5mo ago)22MITPHPPHP &gt;=8.3.0

Since Nov 16Pushed 5mo ago2 watchersCompare

[ Source](https://github.com/cahuk/sitemap-generator)[ Packagist](https://packagist.org/packages/cahuk/sitemap-generator)[ Docs](https://github.com/cahuk/sitemap-generator)[ RSS](/packages/cahuk-sitemap-generator/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

Sitemap Generator
=================

[](#sitemap-generator)

A flexible, high-performance PHP library for generating XML sitemaps of any scale.
Supports multiple URL providers, automatic partitioning, indexing, configurable priorities, frequencies, timestamps, and flexible storage options.

This package follows a clean DDD-inspired structure with separation between Domain, Application, and Infrastructure layers.
---------------------------------------------------------------------------------------------------------------------------

[](#this-package-follows-a-clean-ddd-inspired-structure-with-separation-between-domain-application-and-infrastructure-layers)

🚀 Features
----------

[](#-features)

- Generate XML sitemaps with zero external dependencies
- Multiple sitemap sections (pages, blog, categories, etc.)
- Automatic sitemap partitioning (e.g., split every 50,000 URLs or at your choice)
- Fully customizable URL providers
- Built-in XML renderer
- Local filesystem writer (can be extended to S3, FTP, DB, etc.)
- Strong DTOs, enums, and typed entries
- Simple integration into any PHP project (Symfony, Laravel, custom framework)

🚀 Installation
--------------

[](#-installation)

Install via Composer:

```
composer require cahuk/sitemap-generator
```

\#🧩 Basic Example Below is a real working example based directly on this package:

```
declare(strict_types = 1);

use Cahuk\SitemapGenerator\Application\Service\SitemapGenerator;
use Cahuk\SitemapGenerator\Domain\Model\Enum\ChangeFrequencyEnum;
use Cahuk\SitemapGenerator\Application\Service\SitemapPartitioner;
use Cahuk\SitemapGenerator\Infrastructure\Renderer\XmlSitemapRenderer;
use Cahuk\SitemapGenerator\Application\Dto\SitemapGeneratorSettingDto;
use Cahuk\SitemapGenerator\Infrastructure\Repository\LocalFilesystemWriter;
use Cahuk\SitemapGenerator\Infrastructure\EntryProvider\ArrayUrlEntryProvider;

require_once './vendor/autoload.php';

// 1. Page URLs provider
$pagesEntriesProvider = new ArrayUrlEntryProvider(
    name           : 'sitemap-pages',
    urlEntryData   : [
        'https://example.com/'                      => ['lastModified' => '2025-01-01'],
        'https://example.com/page/our-team'         => ['lastModified' => '2025-01-01'],
        'https://example.com/page/contact-ua'       => ['lastModified' => '2025-01-01'],
        'https://example.com/page/terms'            => ['lastModified' => '2025-01-01'],
        'https://example.com/page/reviews'          => ['lastModified' => '2025-01-01'],
        'https://example.com/page/reviews/john-doe' => ['lastModified' => '2025-01-01'],
    ],
    defPriority    : 0.8,
    defLastModified: new DateTimeImmutable('-10 days'),
    defChangeFreq  : ChangeFrequencyEnum::Monthly,
);

// 2. Blog URLs provider
$blogEntriesProvider = new ArrayUrlEntryProvider(
    name        : 'sitemap-blog',
    urlEntryData: [
        'https://example.com/blog/hot-topic'    => [
            'lastModified' => '2025-01-01',
            'priority'     => 0.7,
            'changeFreq'   => 'daily',
        ],
        'https://example.com/blog/case-study'   => [
            'lastModified' => '2025-01-01',
            'priority'     => 0.5,
            'changeFreq'   => 'monthly',
        ],
        'https://example.com/blog/how-to-start' => [
            'lastModified' => '2025-01-01',
            'priority'     => 0.5,
            'changeFreq'   => 'yearly',
        ],
    ]
);

// 3. Maximum URLs per sitemap file
$maxUrlsPerSitemap = 5000;

// 4. Renderer + filesystem adapter
$sitemapRender = new XmlSitemapRenderer();

// 5. Generator
$sitemapGenerator = new SitemapGenerator(
    repository      : new LocalFilesystemWriter(
        basePath     : "./",
        renderer     : $sitemapRender,
        indexRenderer: $sitemapRender,
    ),
    generatorSetting: new SitemapGeneratorSettingDto(
        host: 'https://freedemo.games/',
    ),
    partitioner     : new SitemapPartitioner(
        $maxUrlsPerSitemap,
        ...[$pagesEntriesProvider, $blogEntriesProvider],
    ),
);

// 6. Generate
$result = $sitemapGenerator->generateSitemap();

echo "Generated:" . PHP_EOL;
$result = [
    'generated_at'            => $result->generatedAt()->format(DATE_W3C),
    'duration'                => $result->getDuration(),
    'urls_count'              => $result->getUrlCount(),
    'path'                    => $result->getPath(),
    'file_names'              => implode(', ', $result->getFileNames()),
    'memory_usage_bytes'      => $result->getMemoryUsedBytes(),
    'memory_peak_usage_bytes' => $result->getMemoryPeakUsageBytes(),
];

foreach ($result as $field => $value) {
    echo "$field: $value " . PHP_EOL;
}
```

\#📁 Output The generator produces:

```
sitemap.xml
sitemap-pages.xml
sitemap-blog.xml
```

With automatic when needed. Example of generated index:

```

        https://freedemo.games/sitemap-pages.xml
        2025-01-01

        https://freedemo.games/sitemap-blog.xml
        2025-01-01

```

\#🧱 URL Entry Providers This package ships out of the box with a ready-to-use provider: ✔ ArrayUrlEntryProvider (built-in) A simple provider that accepts an associative array where:

- keys = URL strings
- values = metadata (lastModified, priority, changeFreq, ...)

```
$pagesEntriesProvider = new ArrayUrlEntryProvider(
    name           : 'sitemap-pages',
    urlEntryData   : [
        'https://example.com/' => [
            'lastModified' => '2025-01-01',
        ],
        'https://example.com/page/our-team' => [
            'lastModified' => '2025-01-01',
        ],
    ],
    defPriority    : 0.8,
    defLastModified: new DateTimeImmutable(),
    defChangeFreq  : ChangeFrequencyEnum::Monthly,
);
```

\#🧩 Creating Custom Providers If the built-in ArrayUrlEntryProvider is not enough, you can easily create your own custom provider by implementing:

```
Cahuk\SitemapGenerator\Domain\Model\EntryProviderInterface
```

This allows you to fetch URLs from:

- a database
- an API
- CMS
- search index
- filesystem
- any dynamic source Example skeleton:

```
