Skip to content

Commit add298e

Browse files
authored
feat: add sitemap generation command (#95)
1 parent 6cf72e9 commit add298e

3 files changed

Lines changed: 595 additions & 2 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Models\Blog;
6+
use App\Models\Subject;
7+
use Illuminate\Console\Command;
8+
use Spatie\Sitemap\Sitemap;
9+
use Spatie\Sitemap\Tags\Url;
10+
11+
class GenerateSitemap extends Command
12+
{
13+
protected $signature = 'seo:sitemap';
14+
15+
protected $description = 'Generate sitemap.xml';
16+
17+
public function handle(): int
18+
{
19+
$sitemap = Sitemap::create();
20+
21+
// Static pages
22+
foreach (
23+
[
24+
'/',
25+
'/ssc',
26+
'/blogs',
27+
'/about-us',
28+
'/privacy-policy',
29+
'/terms-service',
30+
'/content-policy',
31+
'/support',
32+
'/join',
33+
'/guide',
34+
] as $page
35+
) {
36+
$sitemap->add(
37+
Url::create(url($page))
38+
);
39+
}
40+
41+
// Subjects
42+
Subject::orderBy('name')->get()->each(function ($subject) use ($sitemap) {
43+
$sitemap->add(
44+
Url::create(url($subject->slug))
45+
->setLastModificationDate($subject->updated_at)
46+
);
47+
});
48+
49+
// Published blogs
50+
Blog::where('is_published', true)
51+
->orderByDesc('updated_at')
52+
->get()
53+
->each(function ($blog) use ($sitemap) {
54+
$sitemap->add(
55+
Url::create(url("/blogs/{$blog->slug}"))
56+
->setLastModificationDate($blog->updated_at)
57+
);
58+
});
59+
60+
$path = public_path('sitemap.xml');
61+
62+
$sitemap->writeToFile($path);
63+
64+
$this->info("Sitemap generated successfully.");
65+
$this->line($path);
66+
67+
return self::SUCCESS;
68+
}
69+
}

‎composer.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"laravel/tinker": "^3.0",
1818
"laravel/wayfinder": "^0.1.14",
1919
"league/flysystem-aws-s3-v3": "^3.35",
20-
"spatie/laravel-permission": "^8.0"
20+
"spatie/laravel-permission": "^8.0",
21+
"spatie/laravel-sitemap": "^8.0"
2122
},
2223
"require-dev": {
2324
"barryvdh/laravel-debugbar": "^4.4",

0 commit comments

Comments
 (0)