Skip to content

Commit

Permalink
fix: fix sitemap generate (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Feb 18, 2024
1 parent e35df08 commit 5976791
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions app/Console/Commands/GenerateSitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

use App\Models\Name;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\SitemapIndex;
use Spatie\Sitemap\Tags\Sitemap as SitemapTag;
use Spatie\Sitemap\Tags\Url;

class GenerateSitemap extends Command
Expand All @@ -28,21 +32,32 @@ class GenerateSitemap extends Command
*/
public function handle(): void
{
$sitemap = Sitemap::create();
$sitemapIndex = SitemapIndex::create();

Name::where('name', '!=', '_PRENOMS_RARES')
->get()
->each(function (Name $name) use ($sitemap) {
$sitemap->add(
Url::create(route('name.show', [
'id' => $name->id,
'name' => $name->name,
]))
->setPriority(0.9)
->setChangeFrequency(Url::CHANGE_FREQUENCY_MONTHLY)
);
});

$sitemap->writeToFile(public_path('sitemap.xml'));
->chunkById(2000, function (Collection $names, int $key) use ($sitemapIndex) {

$file = 'sitemap_' . Str::padLeft($key, 2, '0') . '.xml';
$sitemap = Sitemap::create();

$names->each(function (Name $name) use ($sitemap) {
$sitemap->add(
Url::create(route('name.show', [
'id' => $name->id,
'name' => Str::lower($name->name),
]))
->setPriority(0.9)
->setChangeFrequency(Url::CHANGE_FREQUENCY_MONTHLY)
->setLastModificationDate($name->updated_at)
);
});

$sitemap->writeToFile(public_path($file));
$sitemap = null;

$sitemapIndex->add(SitemapTag::create(url($file)));
}, 'id');

$sitemapIndex->writeToFile(public_path('sitemap.xml'));
}
}

0 comments on commit 5976791

Please sign in to comment.