Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Symfony Repositemap Bundle Laravel Package

botalaszlo/symfony-repositemap-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. 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
    
  2. First Use Case:

    • Annotate a static route with sitemap=true:
      /**
       * @Route("/about", name="about_page", options={"sitemap"=true})
       */
      public function aboutAction() { ... }
      
    • Access /sitemap.xml to verify generation.
  3. Dynamic Entities:

    • Extend RepoSiteMapBundle\Generator\AbstractGenerator to define entity-based URLs.
    • Annotate entity repositories with @Route and sitemap=true in a custom generator.

Implementation Patterns

Static Pages

  • Annotation-Based: Use @Route(..., options={"sitemap"=true}) on controller actions. Example:
    /**
     * @Route("/blog", name="blog_list", options={"sitemap"=true})
     */
    public function listAction() { ... }
    
  • Priority Handling: Order matters in routing.yml. Place RepoSiteMapBundle after your app’s routes to avoid conflicts.

Dynamic Pages (Entities)

  1. 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);
        }
    }
    
  2. Register Generator: Configure in config.yml:

    repositemap:
        generators:
            post:
                class: AppBundle\Generator\PostGenerator
                priority: 10
    
  3. Entity Repository Integration: Use @Route on repository methods (if supported) or leverage Doctrine lifecycle events to trigger updates.

Workflows

  • Caching: Enable caching in config.yml:
    repositemap:
        cache: true
        cache_lifetime: 86400  # 1 day
    
  • Frequency/Priority: Set changefreq and priority in generator config:
    repositemap:
        generators:
            post:
                changefreq: daily
                priority: 0.8
    

Integration Tips

  • Symfony Flex: If migrating to Symfony 4/5, use auto-wiring and config/packages/repositemap.yaml for cleaner setup.
  • Doctrine Events: Listen to postUpdate/postPersist to refresh sitemap dynamically:
    $dispatcher->addListener(Doctrine\ORM\Events::postUpdate, function($args) {
        $this->get('repositemap.generator_manager')->refresh('post');
    });
    

Gotchas and Tips

Pitfalls

  1. Route Conflicts:

    • Ensure /sitemap.xml doesn’t clash with existing routes. Use _format: xml in your app’s routes to avoid overlaps.
    • Example:
      # app/config/routing.yml
      app_sitemap:
          path:     /sitemap.xml
          defaults: { _controller: RepoSiteMapBundle:Default:sitemap }
          requirements:
              _format: xml
      
  2. Entity Generator Quirks:

    • Lazy Loading: Ensure entity repositories return fully hydrated objects (e.g., findBy() instead of createQueryBuilder() without joins).
    • Circular References: Avoid infinite loops in getUrls() by filtering entities (e.g., WHERE active = 1).
  3. Caching Issues:

    • Clear cache after entity updates:
      php bin/console cache:clear
      
    • For dynamic updates, use cache_lifetime: 0 (disables caching) or implement a custom cache invalidator.
  4. Symfony 4+ Compatibility:

    • The bundle targets Symfony 2.3+. For newer versions:
      • Replace AppKernel with config/bundles.php.
      • Use autoconfigure: true in config/packages/framework.yaml.

Debugging

  • Verify Generation: Check var/log/dev.log for errors or enable debug mode:
    repositemap:
        debug: true
    
  • Dump URLs: Temporarily log generated URLs in your custom generator:
    error_log(print_r($this->getUrls(), true));
    

Extension Points

  1. 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.

  2. 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.

  3. 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;
    });
    

Performance

  • Batch Processing: For large datasets, paginate repository queries:
    $qb = $this->getEntityManager()->createQueryBuilder()
        ->select('p')
        ->from('AppBundle:Post', 'p')
        ->setMaxResults(100);
    
  • Async Updates: Use Symfony’s Messenger component to defer sitemap updates after bulk operations.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware