File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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" ,
You can’t perform that action at this time.
0 commit comments