## Getting Started
### Minimal Setup
1. **Installation**
```bash
composer require presta/sitemap-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Presta\SitemapBundle\PrestaSitemapBundle::class => ['all' => true],
];
Basic Configuration
Add to config/packages/presta_sitemap.yaml:
presta_sitemap:
default_locale: en
base_url: 'https://yourdomain.com'
sitemap:
filename: 'sitemap.xml'
format: 'xml'
First Use Case: Static Sitemap Create a controller to generate and serve the sitemap:
use Presta\SitemapBundle\Generator\SitemapGenerator;
use Symfony\Component\HttpFoundation\Response;
class SitemapController
{
public function generate(SitemapGenerator $generator): Response
{
$sitemap = $generator->generate();
return new Response($sitemap, 200, ['Content-Type' => 'application/xml']);
}
}
Route it in config/routes.yaml:
sitemap:
path: /sitemap.xml
controller: App\Controller\SitemapController::generate
Documentation Start with the official docs and focus on:
Generator\SitemapGenerator classModel\Sitemap and Model\SitemapUrl entitiesDefine Sitemap URLs
Use the SitemapUrl model to define URLs dynamically:
use Presta\SitemapBundle\Model\SitemapUrl;
$url = new SitemapUrl();
$url->setLoc('/blog/post-1');
$url->setLastmod(new \DateTime());
$url->setChangefreq('weekly');
$url->setPriority(0.8);
Register URLs with the Generator
$generator = $this->get('presta.sitemap.generator');
$generator->addUrl($url);
Batch Processing for Large Sitemaps
For sitemaps with >50,000 URLs, split into multiple files using SitemapIndexGenerator:
$indexGenerator = $this->get('presta.sitemap.index_generator');
$indexGenerator->addSitemap('sitemap_part1.xml', new \DateTime());
$indexGenerator->addSitemap('sitemap_part2.xml', new \DateTime());
Doctrine Entity Integration
Use the SitemapUrlRepository to fetch URLs from a database:
$urls = $this->get('presta.sitemap.url.repository')->findAll();
foreach ($urls as $url) {
$generator->addUrl($url);
}
Event-Driven Updates
Trigger sitemap regeneration on entity events (e.g., PostUpdatedEvent):
use Presta\SitemapBundle\Event\SitemapEvent;
public function onPostUpdated(PostUpdatedEvent $event)
{
$dispatcher = $this->get('event_dispatcher');
$dispatcher->dispatch(new SitemapEvent('post.updated', $event->getPost()));
}
Twig Templates for Customization
Override default templates in templates/PrestaSitemapBundle/:
{# templates/PrestaSitemapBundle/Sitemap/sitemap.xml.twig #}
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{% for url in urls %}
<url>
<loc>{{ url.loc }}</loc>
{% if url.lastmod %}<lastmod>{{ url.lastmod|date('c') }}</lastmod>{% endif %}
{% if url.changefreq %}<changefreq>{{ url.changefreq }}</changefreq>{% endif %}
{% if url.priority %}<priority>{{ url.priority }}</priority>{% endif %}
</url>
{% endfor %}
</urlset>
Caching for Performance Cache generated sitemaps using Symfony’s cache system:
# config/packages/presta_sitemap.yaml
presta_sitemap:
cache:
enabled: true
lifetime: 3600 # 1 hour
Multi-Language Support Generate locale-specific sitemaps:
presta_sitemap:
locales:
en:
filename: 'sitemap_en.xml'
fr:
filename: 'sitemap_fr.xml'
URL Encoding Issues
loc values are absolute URLs (include base_url from config).UrlGeneratorInterface to generate correct paths:
$url = $this->get('router')->generate('route_name', ['id' => 1], UrlGeneratorInterface::ABSOLUTE_URL);
DateTime Formatting
lastmod must be in RFC 3339 format (YYYY-MM-DDTHH:MM:SS+HH:MM). Use:
$url->setLastmod(new \DateTime('now', new \DateTimeZone('UTC')));
XML Validation Errors
xmlns namespace in sitemapindex.<loc> entries.priority values (must be 0.0–1.0).Caching Headaches
php bin/console cache:clear
presta_sitemap:
cache:
enabled: false
Large Sitemap Performance
$qb = $this->get('presta.sitemap.url.repository')->createQueryBuilder('u');
$qb->setMaxResults(1000);
$urls = $qb->getQuery()->getResult();
Enable Verbose Logging
presta_sitemap:
debug: true
Check logs for generated XML snippets.
Validate XML Output Use an online validator like XML Validation or CLI:
xmllint --noout --schema http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd sitemap.xml
Check for Deprecated Methods
Custom URL Providers
Implement Presta\SitemapBundle\Provider\UrlProviderInterface:
class CustomUrlProvider implements UrlProviderInterface
{
public function getUrls(): array
{
return [
new SitemapUrl('/custom-path', new \DateTime()),
];
}
}
Register as a service:
services:
App\Provider\CustomUrlProvider:
tags:
- { name: presta.sitemap.url_provider }
Override Generators
Extend SitemapGenerator or SitemapIndexGenerator:
class CustomSitemapGenerator extends SitemapGenerator
{
protected function getTemplate(): string
{
return '@App/Sitemap/custom_sitemap.xml.twig';
}
}
Replace the service in config/services.yaml:
Presta\SitemapBundle\Generator\SitemapGenerator: '@App\Generator\CustomSitemapGenerator'
Add Custom XML Attributes
Extend SitemapUrl or use a custom model:
class ExtendedSitemapUrl extends SitemapUrl
{
private $imageLoc;
// Getters/setters...
}
Update the Twig template to include new fields.
Integrate with API Platform
How can I help you explore Laravel packages today?