PHP code example of cahuk / sitemap-generator
1. Go to this page and download the library: Download cahuk/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/ */
cahuk / sitemap-generator example snippets
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;
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;
}
$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,
);
Cahuk\SitemapGenerator\Domain\Model\EntryProviderInterface
declare(strict_types = 1);
namespace Cahuk\SitemapGenerator\Infrastructure\EntryProvider;
use Override;
use PDO;
use Exception;
use DateTimeImmutable;
use Cahuk\SitemapGenerator\Domain\Model\UrlEntry;
use Cahuk\SitemapGenerator\Domain\Model\ValueObject\Url;
use Cahuk\SitemapGenerator\Domain\Model\ValueObject\Priority;
use Cahuk\SitemapGenerator\Domain\Model\Enum\ChangeFrequencyEnum;
use Cahuk\SitemapGenerator\Domain\Model\ValueObject\ChangeFrequency;
use Cahuk\SitemapGenerator\Domain\EntryProvider\UrlEntryProviderInterface;
/**
* Class MyCustomProvider
*
* Example of a custom URL provider powered by database entries.
*/
final class MyCustomProvider implements UrlEntryProviderInterface
{
private string $name;
private PDO $pdo;
private ?float $defPriority;
private ?DateTimeImmutable $defLastModified;
private ?ChangeFrequencyEnum $defChangeFreq;
/**
* @param string $name
* @param PDO $pdo
* @param float|null $defPriority
* @param DateTimeImmutable|null $defLastModified
* @param ChangeFrequencyEnum|null $defChangeFreq
*/
public function __construct(
string $name,
PDO $pdo,
?float $defPriority = null,
?DateTimeImmutable $defLastModified = null,
?ChangeFrequencyEnum $defChangeFreq = null,
) {
$this->name = $name;
$this->pdo = $pdo;
$this->defPriority = $defPriority;
$this->defLastModified = $defLastModified;
$this->defChangeFreq = $defChangeFreq;
}
#[Override]
public function getName(): string
{
return $this->name;
}
/**
* Returns iterable list of UrlEntry objects from the database.
*
* Your database table must contain at least:
* - url (string)
* - updated_at (optional, datetime)
* - priority (optional float)
* - change_freq (optional string matching ChangeFrequencyEnum)
*
* @throws Exception
*/
#[Override]
public function getUrlEntry(): iterable
{
$stmt = $this->pdo->query("
SELECT
url,
updated_at,
priority,
change_freq
FROM sitemap_urls
");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
// URL value object
$url = new Url($row['url']);
// Priority
$priority = isset($row['priority']) ?
new Priority((float)$row['priority']) :
($this->defPriority ? new Priority($this->defPriority) : null);
// Last modified
$lastModified = !empty($row['updated_at'])
? new DateTimeImmutable($row['updated_at'])
: $this->defLastModified;
// Change frequency
$changeFreq = !empty($row['change_freq'])
? new ChangeFrequency(ChangeFrequencyEnum::from($row['change_freq']))
: ($this->defChangeFreq ? new ChangeFrequency($this->defChangeFreq) : null);
yield new UrlEntry(
url : $url,
lastModified: $lastModified,
priority : $priority,
changeFreq : $changeFreq,
);
}
}
}
xml
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>https://freedemo.games/sitemap-pages.xml</loc>
<lastmod>2025-01-01</lastmod>
</sitemap>
<sitemap>
<loc>https://freedemo.games/sitemap-blog.xml</loc>
<lastmod>2025-01-01</lastmod>
</sitemap>
</sitemapindex>