Kritim Yantra
Apr 16, 2025
In this comprehensive guide, we'll explore how to use Spatie's Laravel-Sitemap package in Laravel 12 to generate XML sitemaps that boost your website's SEO performance and search engine visibility.
Sitemaps help search engines:
Google reports that websites with sitemaps get indexed 50% faster on average.
composer create-project laravel/laravel seo-project
cd seo-project
composer require spatie/laravel-sitemap
php artisan vendor:publish --provider="Spatie\Sitemap\SitemapServiceProvider" --tag="config"
Create a route in routes/web.php
:
use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\Tags\Url;
Route::get('/sitemap', function () {
return Sitemap::create()
->add(Url::create('/'))
->add(Url::create('/about'))
->add(Url::create('/contact'))
->writeToFile(public_path('sitemap.xml'));
});
Visit /sitemap
to generate:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://example.com/</loc>
<lastmod>2024-03-20T00:00:00+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
<!-- More URLs -->
</urlset>
use App\Models\Post;
Sitemap::create()
->add(Post::all()->map(function (Post $post) {
return Url::create("/posts/{$post->slug}")
->setLastModificationDate($post->updated_at);
}));
collect(Route::getRoutes()->getRoutesByName())
->filter(fn ($route) => str_starts_with($route->getName(), 'public.'))
->each(fn ($route) => Sitemap::add(Url::create($route->uri())));
Url::create('/pricing')
->setPriority(1.0)
->setChangeFrequency(Url::CHANGE_FREQUENCY_MONTHLY);
Page Type | Priority | Frequency |
---|---|---|
Homepage | 1.0 | Daily |
Product Pages | 0.9 | Weekly |
Blog Posts | 0.8 | Monthly |
Archive Pages | 0.4 | Yearly |
// In app/Console/Commands/GenerateSitemaps.php
Sitemap::create()
->add(Url::create('/products'))
->writeToFile(public_path('sitemap-products.xml'));
Sitemap::create()
->add(BlogPost::all())
->writeToFile(public_path('sitemap-blog.xml'));
Url::create('/gallery')
->addImage(
Image::create('/images/photo1.jpg')
->setTitle('Summer Vacation')
->setLicense('https://creativecommons.org/licenses/by/4.0/')
);
Url::create('/videos')
->addVideo(
Video::create()
->setTitle('Product Demo')
->setDescription('5-minute overview')
->setContentLocation('/videos/demo.mp4')
);
SitemapIndex::create()
->add('/sitemap-products.xml')
->add('/sitemap-blog.xml')
->writeToFile(public_path('sitemap.xml'));
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>https://example.com/sitemap-products.xml</loc>
</sitemap>
<sitemap>
<loc>https://example.com/sitemap-blog.xml</loc>
</sitemap>
</sitemapindex>
php artisan make:command GenerateSitemap
In app/Console/Commands/GenerateSitemap.php
:
protected $signature = 'sitemap:generate';
public function handle()
{
SitemapGenerator::create(config('app.url'))
->writeToFile(public_path('sitemap.xml'));
}
Schedule in app/Console/Kernel.php
:
$schedule->command('sitemap:generate')->daily();
Url::create('/')
->addAlternate('/fr', 'fr')
->addAlternate('/es', 'es');
For sites with 50,000+ URLs:
sitemap_index
approach# Using curl
curl https://example.com/sitemap.xml | xmllint --noout --schema sitemap.xsd -
By implementing Spatie's Laravel-Sitemap:
✅ Improved search engine visibility
✅ Faster indexing of new content
✅ Better crawl efficiency
✅ Media content discovery
✅ Multi-language support
Pro Tip: Combine with Laravel SEO Packages for maximum impact.
Ready to boost your SEO? Implement your sitemap today! 🚀
Transform from beginner to Laravel expert with our personalized Coaching Class starting June 11, 2025. Limited enrollment ensures focused attention.
1-hour personalized coaching
Build portfolio applications
Industry-standard techniques
Interview prep & job guidance
Complete your application to secure your spot
Thank you for your interest in our Laravel mentorship program. We'll contact you within 24 hours with next steps.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google