Installation:
composer require laravelium/sitemap
For Laravel, publish the config (optional):
php artisan vendor:publish --provider="Laravelium\Sitemap\SitemapServiceProvider"
Basic Usage: Define a route to generate the sitemap:
use Laravelium\Sitemap\Facades\Sitemap;
Route::get('/sitemap.xml', function () {
return Sitemap::create()
->add(url('/'), 1.0, 'daily')
->add(url('/about'), 0.8, 'weekly')
->render('xml');
});
First Use Case:
Generate a simple XML sitemap for core routes. Test locally by visiting /sitemap.xml.
Dynamic Sitemap Generation: Use closures to fetch dynamic URLs (e.g., blog posts):
Sitemap::create()
->add(url('/posts'), 0.7, 'monthly')
->add(url('/posts/{id}'), 0.9, 'weekly', function ($sitemap) {
return Post::all()->map(fn ($post) => $post->slug);
});
Multi-Language Support:
Add hreflang tags for translated content:
$sitemap->add(url('/post'), 1.0, 'daily')
->setLanguage('en')
->addAlternate(url('/post-es'), 'es');
Image/Video Sitemaps: Extend the base class for rich media:
use Laravelium\Sitemap\SitemapGenerator;
use Laravelium\Sitemap\Items\Image;
class VideoSitemap extends SitemapGenerator {
public function addVideo($url, $title, $description) {
return $this->add($url, 1.0, 'daily')
->addVideo($title, $description, url('/video-thumbnail.jpg'));
}
}
Caching: Cache generated sitemaps for performance:
Route::get('/sitemap.xml', function () {
return Cache::remember('sitemap-xml', now()->addHours(6), function () {
return Sitemap::create()->add(...)->render('xml');
});
});
Route::get() for public sitemaps or Route::middleware(['auth']) for private ones.dispatch(new GenerateSitemapJob());
Sitemap facade in unit tests:
$sitemap = Mockery::mock(Laravelium\Sitemap\Facades\Sitemap::class);
$sitemap->shouldReceive('render')->andReturn('<xml>...</xml>');
URL Validation:
url('/path') instead of /path). Relative URLs may break validation.str()->startsWith('http') to validate URLs before adding them.Change Frequency:
always for large collections (e.g., product pages). Use weekly or monthly instead.changefreq if lastmod is missing.Memory Limits:
Sitemap::create()->setLimit(3000)->render('xml'); // Max 3000 URLs per file
Caching Headers:
Cache-Control headers to prevent proxy caching issues:
return response(Sitemap::render('xml'))
->header('Cache-Control', 'public, max-age=86400');
try-catch to log issues:
try {
return Sitemap::create()->add(...)->render('xml');
} catch (\Exception $e) {
Log::error('Sitemap generation failed: ' . $e->getMessage());
return response('Error generating sitemap.', 500);
}
Custom Items:
Extend Laravelium\Sitemap\Items\Url for custom metadata:
class CustomUrl extends Url {
public function setCustomTag($name, $value) {
$this->customTags[$name] = $value;
}
}
Adapters:
Override the default XmlWriter adapter for custom formats:
use Laravelium\Sitemap\Adapters\AdapterInterface;
class JsonAdapter implements AdapterInterface {
public function render(array $items) {
return json_encode($items);
}
}
Service Provider: Bind custom sitemap classes in the service provider:
$this->app->bind('custom.sitemap', function () {
return new CustomSitemap();
});
0.5 if not specified. Explicitly set priorities for critical pages (e.g., 1.0 for homepages).lastmod timestamps use UTC to avoid timezone-related issues:
$sitemap->add(url('/post'), 1.0, 'daily', now()->tz('UTC'));
How can I help you explore Laravel packages today?