Skip to content

Commit 28f48ea

Browse files
committed
fix(sitemap generation) update old sitemap path
1 parent 22d8a5b commit 28f48ea

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

app/Console/Commands/GenerateSitemap.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Carbon\Carbon;
88
use Exception;
99
use Illuminate\Console\Command;
10-
use Illuminate\Support\Str;
1110
use Spatie\Sitemap\Sitemap;
1211
use Spatie\Sitemap\Tags\Url;
1312

@@ -53,26 +52,33 @@ public function handle()
5352

5453
// 2. Add Posts
5554
$this->info('Adding posts...');
56-
Post::query()
55+
56+
Post::with('user')
5757
// ->where('is_published', true)
5858
// ->where('moderation_status', 'approved')
5959
// ->whereNotNull('slug')
6060
->orderBy('created_at', 'desc')
6161
->chunk(200, function ($posts) use ($sitemap) {
6262
foreach ($posts as $post) {
63-
$slug = Str::slug($post->question);
6463
try {
65-
$url = route('posts.show.user-scoped', ['username' => $post->user->username, 'post' => $post->id]);
66-
$sitemap->add(Url::create($url)
67-
->setLastModificationDate($post->updated_at)
68-
->setChangeFrequency(Url::CHANGE_FREQUENCY_WEEKLY)
69-
->setPriority(0.9));
70-
// $post->update(['slug' => $slug]);
64+
$url = route('posts.show.user-scoped', [
65+
'username' => $post->user->username,
66+
'post' => $post->id,
67+
]);
68+
69+
$sitemap->add(
70+
Url::create($url)
71+
->setLastModificationDate($post->updated_at)
72+
->setChangeFrequency(Url::CHANGE_FREQUENCY_WEEKLY)
73+
->setPriority(0.9)
74+
);
75+
7176
} catch (Exception $e) {
7277
$this->error("Could not generate URL for Post ID {$post->id} ('{$post->question}'): " . $e->getMessage());
7378
}
7479
}
7580
});
81+
7682
$this->info('Posts added.');
7783

7884
// 3. Add User Profiles

app/Http/Controllers/SitemapController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function index(): StreamedResponse
1515
echo View::make('sitemap.partials.header')->render();
1616
echo View::make('sitemap.partials.static')->render();
1717

18-
Post::chunkById(2000, function ($posts) {
18+
Post::with('user')->chunkById(2000, function ($posts) {
1919
echo View::make('sitemap.partials.posts', ['posts' => $posts])->render();
2020
});
2121

resources/views/sitemap/partials/posts.blade.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
@foreach ($posts as $post)
22
<url>
3-
<loc>{{ url('p/' . $post->id . '/' . Illuminate\Support\Str::slug($post->question)) }}</loc>
3+
<loc>{{ route('posts.show.user-scoped', ['username' => $post->user->username, 'post' => $post->id]) }}</loc>
44
<lastmod>{{ $post->updated_at->tz('Asia/Tashkent')->toAtomString() }}</lastmod>
55
<changefreq>weekly</changefreq>
66
<priority>0.9</priority>
7+
78
@if ($post->option_one_image)
89
<image:image>
910
<image:loc>{{ asset('storage/' . $post->option_one_image) }}</image:loc>
10-
<image:title>{{ htmlspecialchars($post->option_one_title) }}</image:title>
11-
<image:caption>{{ htmlspecialchars($post->question) }}</image:caption>
11+
<image:title>{{ $post->option_one_title }}</image:title>
12+
<image:caption>{{ $post->question }}</image:caption>
1213
</image:image>
1314
@endif
15+
1416
@if ($post->option_two_image)
1517
<image:image>
1618
<image:loc>{{ asset('storage/' . $post->option_two_image) }}</image:loc>
17-
<image:title>{{ htmlspecialchars($post->option_two_title) }}</image:title>
18-
<image:caption>{{ htmlspecialchars($post->question) }}</image:caption>
19+
<image:title>{{ $post->option_two_title }}</image:title>
20+
<image:caption>{{ $post->question }}</image:caption>
1921
</image:image>
2022
@endif
2123
</url>

0 commit comments

Comments
 (0)