darvinstudio/darvin-sitemap-bundle
Installation:
composer.json under "require":
"darvinstudio/darvin-sitemap-bundle": "1.0.*"
composer update darvinstudio/darvin-sitemap-bundle.AppKernel.php:
new Darvin\SitemapBundle\DarvinSitemapBundle(),
app/config/routing.yml:
darvin_sitemap:
resource: "@DarvinSitemapBundle/Resources/config/routing.yml"
prefix: /
First Use Case:
SitemapUrlProviderInterface to define URLs for your sitemap./sitemap.xml (default route).SitemapUrlProviderInterface and SitemapUrl classes in the src/Darvin/SitemapBundle/Url namespace.DarvinSitemapBundle/Resources/config/routing.yml for the default route (/sitemap.xml).DarvinSitemapBundle/Resources/config/services.yml for default service definitions.Define Providers:
SitemapUrlProviderInterface for each logical group of URLs (e.g., BlogSitemapProvider, ProductSitemapProvider).class BlogSitemapProvider implements SitemapUrlProviderInterface
{
public function getSitemapUrls()
{
return array_map(function ($post) {
return new SitemapUrl(
$this->generateUrl('post_show', ['slug' => $post->slug]),
$post->updatedAt,
'daily',
$post->priority
);
}, $this->blogRepository->findAll());
}
}
Tag Services:
darvin_sitemap.url_provider:
# app/config/services.yml
services:
app.sitemap.blog_provider:
class: AppBundle\Sitemap\BlogSitemapProvider
tags:
- { name: darvin_sitemap.url_provider }
Dynamic URL Generation:
UrlGeneratorInterface to generate routes dynamically:
$urlGenerator = $this->container->get('router');
$url = $urlGenerator->generate('route_name', ['param' => 'value']);
Sitemap Indexing:
sitemap1.xml, sitemap2.xml) and reference them in a sitemap index (sitemap.xml).class SitemapIndexProvider implements SitemapUrlProviderInterface
{
public function getSitemapUrls()
{
return [
new SitemapUrl('/sitemap1.xml', new \DateTime(), 'always'),
new SitemapUrl('/sitemap2.xml', new \DateTime(), 'always'),
];
}
}
Caching:
$cache = $this->container->get('cache.app');
if (!$cache->has('sitemap_content')) {
$content = $this->generateSitemap();
$cache->set('sitemap_content', $content, 3600); // Cache for 1 hour
}
Doctrine Integration:
SitemapUrl objects. Example:
$products = $this->productRepository->findBy([], ['createdAt' => 'DESC']);
$urls = array_map(function ($product) {
return new SitemapUrl(
$this->generateUrl('product_show', ['id' => $product->id]),
$product->updatedAt,
'weekly',
0.8
);
}, $products);
Translation:
<loc> tags for multilingual sites).Testing:
SitemapUrlProviderInterface in unit tests to verify URL generation logic:
$provider = $this->createMock(SitemapUrlProviderInterface::class);
$provider->method('getSitemapUrls')->willReturn([new SitemapUrl('test', new \DateTime())]);
$this->container->set('app.sitemap.test_provider', $provider);
Deprecated Symfony2:
Routing Conflicts:
/sitemap.xml may conflict with existing routes. Override the route in your routing.yml:
darvin_sitemap:
resource: "@DarvinSitemapBundle/Resources/config/routing.yml"
prefix: /custom-prefix
XML Validation:
Performance:
Service Tagging:
darvin_sitemap.url_provider will silently exclude it from the sitemap. Verify tags with:
php bin/console debug:container --tag=darvin_sitemap.url_provider
Log Provider Output:
$urls = $this->getSitemapUrls();
foreach ($urls as $url) {
$this->logger->info('Sitemap URL: ' . $url->getLoc());
}
Check Bundle Configuration:
AppKernel.php and routing is imported. Test with:
php bin/console debug:router | grep sitemap
Validate XML Output:
/sitemap.xml for syntax errors. Use browser dev tools or curl:
curl -v http://your-site.com/sitemap.xml
Extend SitemapUrl:
class CustomSitemapUrl extends SitemapUrl
{
private $customData;
public function __construct($loc, \DateTime $lastmod, $changefreq, $priority, $customData)
{
parent::__construct($loc, $lastmod, $changefreq, $priority);
$this->customData = $customData;
}
public function getCustomData()
{
return $this->customData;
}
}
Use Events:
darvin_sitemap.generate event to modify the sitemap before rendering. Example in a subscriber:
public function onSitemapGenerate(GetResponseEvent $event)
{
$sitemap = $event->getSitemap();
$sitemap->addUrl(new SitemapUrl('http://example.com/extra-page'));
}
Custom Templates:
DarvinSitemapBundle/Resources/views/Sitemap/sitemap.xml.twig.Prioritize Critical Pages:
1.0) to key pages (e.g., homepage, checkout) and lower priority (0.1) to less critical content.Monitor Changes:
changefreq parameter wisely:
always: Critical pages (e.g., homepage).hourly: News or time-sensitive content.daily: Blog posts or product pages.weekly: Static pages like "About Us."monthly: Archive pages.How can I help you explore Laravel packages today?