Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

benjamincrozat/blog-v5

126 stars
1 code files
View benjamincrozat/blog-v5 on GitHub

app/Console/Commands/GenerateSitemapCommand.php

Open in GitHub
use App\Models\Post;
use App\Models\Category;
use Spatie\Sitemap\Sitemap;
use Illuminate\Console\Command;
 
class GenerateSitemapCommand extends Command
{
protected $signature = 'app:generate-sitemap';
 
protected $description = 'Generate the sitemap.';
 
public function handle() : void
{
$sitemap = Sitemap::create(config('app.url'));
 
$sitemap->add(route('home'));
 
$sitemap->add(route('posts.index'));
 
Post::query()
->published()
->cursor()
->each(fn (Post $post) => $sitemap->add(route('posts.show', $post)));
 
$sitemap->add(route('categories.index'));
 
Category::query()
->cursor()
->each(fn (Category $category) => $sitemap->add(route('categories.show', $category)));
 
$sitemap->add(route('links.index'));
 
$sitemap->writeToFile($path = public_path('sitemap.xml'));
 
$this->info("Sitemap generated successfully at $path");
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.