## 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
});
$generator = new SitemapGenerator();
$sitemap = $generator->addRoute('/home')->addRoute('/about')->generate();
Outputs XML compliant with sitemap protocols.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());
}
}
Priority & Frequency Annotate routes with metadata (if supported):
$generator->addRoute('/blog/post', [
'priority' => 0.8,
'changefreq' => 'weekly',
]);
Multi-Language Sitemaps Use middleware to segment routes by locale:
$generator->addRoute('/fr/blog', ['lang' => 'fr']);
// Later, filter routes by language in a service.
Caching
Cache generated sitemaps (Laravel’s Cache facade):
$cacheKey = 'sitemap_xml';
$sitemap = Cache::remember($cacheKey, now()->addHours(1), function () {
return $generator->generate();
});
$this->app->singleton(SitemapGenerator::class, function ($app) {
return new SitemapGenerator();
});
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!');
}
}
Symfony Dependency
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.)Route Naming Conflicts
Route::name('home')) may not map 1:1 to Symfony’s expectations. Pre-process routes:
$routeName = Route::getRoutes()->getByName('home')?->uri();
XML Validation
xmllint --noout --schema http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd sitemap.xml
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. |
Custom Generators Extend the base generator:
class CustomSitemapGenerator extends SitemapGenerator {
public function addVideoSitemap(string $url, string $title) {
// Implement VideoSitemap protocol.
}
}
Event Listeners
Trigger sitemap regeneration on route changes (e.g., via RouteRegistered events in Laravel).
Dynamic URL Providers
Override addRoute() to fetch URLs from a database or API:
$generator->addRoute('/products/' . $product->slug);
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.)*
How can I help you explore Laravel packages today?