botalaszlo/symfony-repositemap-bundle
Installation:
composer require botalaszlo/symfony-repositemap-bundle:dev-master
Add to AppKernel.php:
new RepoSiteMapBundle\RepoSiteMapBundle(),
Register routes in app/config/routing.yml:
RepoSiteMapBundle:
resource: "@RepoSiteMapBundle/Controller/"
type: annotation
First Use Case:
sitemap=true:
/**
* @Route("/about", name="about_page", options={"sitemap"=true})
*/
public function aboutAction() { ... }
/sitemap.xml to verify generation.Dynamic Entities:
RepoSiteMapBundle\Generator\AbstractGenerator to define entity-based URLs.@Route and sitemap=true in a custom generator.@Route(..., options={"sitemap"=true}) on controller actions.
Example:
/**
* @Route("/blog", name="blog_list", options={"sitemap"=true})
*/
public function listAction() { ... }
routing.yml. Place RepoSiteMapBundle after your app’s routes to avoid conflicts.Custom Generator:
Create a class extending AbstractGenerator:
use RepoSiteMapBundle\Generator\AbstractGenerator;
class PostGenerator extends AbstractGenerator {
protected function getUrls() {
$posts = $this->getEntityManager()->getRepository('AppBundle:Post')->findAll();
return array_map(function($post) {
return ['url' => $this->generateUrl('post_show', ['id' => $post->getId()]), 'lastmod' => $post->getUpdatedAt()];
}, $posts);
}
}
Register Generator:
Configure in config.yml:
repositemap:
generators:
post:
class: AppBundle\Generator\PostGenerator
priority: 10
Entity Repository Integration:
Use @Route on repository methods (if supported) or leverage Doctrine lifecycle events to trigger updates.
config.yml:
repositemap:
cache: true
cache_lifetime: 86400 # 1 day
changefreq and priority in generator config:
repositemap:
generators:
post:
changefreq: daily
priority: 0.8
auto-wiring and config/packages/repositemap.yaml for cleaner setup.postUpdate/postPersist to refresh sitemap dynamically:
$dispatcher->addListener(Doctrine\ORM\Events::postUpdate, function($args) {
$this->get('repositemap.generator_manager')->refresh('post');
});
Route Conflicts:
/sitemap.xml doesn’t clash with existing routes. Use _format: xml in your app’s routes to avoid overlaps.# app/config/routing.yml
app_sitemap:
path: /sitemap.xml
defaults: { _controller: RepoSiteMapBundle:Default:sitemap }
requirements:
_format: xml
Entity Generator Quirks:
findBy() instead of createQueryBuilder() without joins).getUrls() by filtering entities (e.g., WHERE active = 1).Caching Issues:
php bin/console cache:clear
cache_lifetime: 0 (disables caching) or implement a custom cache invalidator.Symfony 4+ Compatibility:
AppKernel with config/bundles.php.autoconfigure: true in config/packages/framework.yaml.var/log/dev.log for errors or enable debug mode:
repositemap:
debug: true
error_log(print_r($this->getUrls(), true));
Custom XML Templates:
Override the default template by extending the DefaultController:
class CustomSitemapController extends \RepoSiteMapBundle\Controller\DefaultController {
public function sitemapAction() {
return $this->render('AppBundle:Sitemap:custom.xml.twig', [
'urls' => $this->getUrls()
]);
}
}
Update routing to point to your controller.
Add Metadata:
Extend the Url class to include custom attributes (e.g., image, video):
class ExtendedUrl extends \RepoSiteMapBundle\Model\Url {
public $image;
}
Update your generator to populate these fields.
Multi-Language Support:
Use the locale parameter in @Route and filter URLs by locale in generators:
$urls = array_filter($urls, function($url) {
return strpos($url['url'], '_' . $locale) !== false;
});
$qb = $this->getEntityManager()->createQueryBuilder()
->select('p')
->from('AppBundle:Post', 'p')
->setMaxResults(100);
How can I help you explore Laravel packages today?