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

Sitemap Bundle Laravel Package

christiana/sitemap-bundle

View on GitHub
Deep Wiki
Context7
## Getting Started

### Minimal Setup
1. **Installation**
   Run `composer require christiana/sitemap-bundle` in your Laravel project directory.
   *(Note: While this is a Symfony bundle, Laravel users can integrate it via a bridge like [`symfony/console-bridge`](https://github.com/symfony/console-bridge) or by leveraging its core logic in a Laravel service provider.)*

2. **Configuration**
   Publish the bundle’s config (if available) or manually define routes in `routes/web.php`:
   ```php
   use Christina\SitemapBundle\Generator\SitemapGenerator;

   Route::get('/sitemap.xml', function () {
       $generator = new SitemapGenerator();
       return $generator->generate(); // Basic usage; see below for customization
   });
  1. First Use Case Generate a static sitemap for your Laravel routes:
    $generator = new SitemapGenerator();
    $sitemap = $generator->addRoute('/home')->addRoute('/about')->generate();
    
    Outputs XML compliant with sitemap protocols.

Implementation Patterns

Core Workflows

  1. Dynamic Route Integration Hook into Laravel’s route collection to auto-discover routes:

    use Illuminate\Support\Facades\Route;
    use Christina\SitemapBundle\Generator\SitemapGenerator;
    
    $generator = new SitemapGenerator();
    foreach (Route::getRoutes()->getRoutes() as $route) {
        if ($route->getName() && !str_starts_with($route->getPath(), '/api')) {
            $generator->addRoute($route->uri());
        }
    }
    
  2. Priority & Frequency Annotate routes with metadata (if supported):

    $generator->addRoute('/blog/post', [
        'priority' => 0.8,
        'changefreq' => 'weekly',
    ]);
    
  3. Multi-Language Sitemaps Use middleware to segment routes by locale:

    $generator->addRoute('/fr/blog', ['lang' => 'fr']);
    // Later, filter routes by language in a service.
    
  4. Caching Cache generated sitemaps (Laravel’s Cache facade):

    $cacheKey = 'sitemap_xml';
    $sitemap = Cache::remember($cacheKey, now()->addHours(1), function () {
        return $generator->generate();
    });
    

Integration Tips

  • Laravel Service Provider Register the generator as a singleton:
    $this->app->singleton(SitemapGenerator::class, function ($app) {
        return new SitemapGenerator();
    });
    
  • Artisan Command Create a custom command for CLI generation:
    use Christina\SitemapBundle\Generator\SitemapGenerator;
    
    class GenerateSitemapCommand extends Command
    {
        protected $signature = 'sitemap:generate';
        public function handle(SitemapGenerator $generator) {
            file_put_contents(public_path('sitemap.xml'), $generator->generate());
            $this->info('Sitemap generated!');
        }
    }
    

Gotchas and Tips

Pitfalls

  1. Symfony Dependency

    • The bundle assumes Symfony’s HttpFoundation and Routing components. For Laravel, mock these or use adapters:
      use Symfony\Component\HttpFoundation\Request;
      use Symfony\Component\Routing\RouteCollection;
      
      (Example: Use symfony/routing via Composer.)
  2. Route Naming Conflicts

    • Laravel’s named routes (e.g., Route::name('home')) may not map 1:1 to Symfony’s expectations. Pre-process routes:
      $routeName = Route::getRoutes()->getByName('home')?->uri();
      
  3. XML Validation

    • Generated XML must comply with sitemaps.org. Validate with:
      xmllint --noout --schema http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd sitemap.xml
      

Debugging

  • Verbose Output Enable debug mode in the generator (if supported) or log raw routes:

    $generator->setDebug(true);
    // Or log routes before generation:
    \Log::debug('Sitemap routes:', $generator->getRoutes());
    
  • Common Errors

    Error Solution
    Class not found Ensure Symfony components are installed.
    Empty sitemap Check route discovery logic.
    Invalid XML Validate against schema.

Extension Points

  1. Custom Generators Extend the base generator:

    class CustomSitemapGenerator extends SitemapGenerator {
        public function addVideoSitemap(string $url, string $title) {
            // Implement VideoSitemap protocol.
        }
    }
    
  2. Event Listeners Trigger sitemap regeneration on route changes (e.g., via RouteRegistered events in Laravel).

  3. Dynamic URL Providers Override addRoute() to fetch URLs from a database or API:

    $generator->addRoute('/products/' . $product->slug);
    
  4. Sitemap Indexing Generate a sitemap-index.xml for large sites:

    $indexGenerator = new SitemapIndexGenerator();
    $indexGenerator->addSitemap('/sitemap-posts.xml');
    $indexGenerator->addSitemap('/sitemap-pages.xml');
    

*(Note: Since the package is minimal and lacks active maintenance, some assumptions are made about its functionality based on the README. Always verify against the actual codebase.)*
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.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle