Last active
June 16, 2024 12:19
-
-
Save abenevaut/25d28d73662d184cc84ca04b4c398a43 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
use Spatie\Sitemap\SitemapGenerator; | |
class GenerateSitemap extends Command | |
{ | |
protected $signature = 'app:generate-sitemap'; | |
protected $description = 'Generate sitemap'; | |
public function handle() | |
{ | |
$sitemap = SitemapGenerator::create(config('app.url')) | |
->getSitemap(); | |
$sitemap->add( | |
Url::create(route('terms.index')) | |
->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY) | |
->setPriority(0) | |
); | |
$sitemap->add( | |
Url::create(route('contact.index')) | |
->setChangeFrequency(Url::CHANGE_FREQUENCY_DAILY) | |
->setPriority(0.2) | |
); | |
$projects = (new Project())->paginate(12); | |
if ($projects->lastPage() > 0) { | |
foreach (range(1, $projects->lastPage()) as $index) { | |
$url = route('projects.index', $index === 1 ? [] : ['page' => $index]); | |
$sitemap->add( | |
Url::create($url) | |
->setChangeFrequency(Url::CHANGE_FREQUENCY_DAILY) | |
->setPriority(0.5) | |
); | |
} | |
} | |
$sitemap->writeToFile(public_path('sitemap.xml')); | |
} | |
} |
Hello @leGeekMaka,
Oui, c'est bien ça; Toutes tes routes uniques devront être ajoutées unitairement à ton sitemap via:
$sitemap->add(
Url::create(route('terms.index'))
);
Pour illustrer, si on ajoute à mon exemple une page contact, une page qui présente l'équipe de ton site et une page de remerciement, je les ajouterais comme suit à mon sitemap:
$sitemap->add(
Url::create(route('contact.index'))
->setChangeFrequency(Url::CHANGE_FREQUENCY_DAILY)
->setPriority(0.5)
);
$sitemap->add(
Url::create(route('team.index'))
->setChangeFrequency(Url::CHANGE_FREQUENCY_MONTHLY)
->setPriority(0.2)
);
$sitemap->add(
Url::create(route('thanks.index'))
->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
->setPriority(0.1)
);
Le README du repository propose même une syntaxe chainée:
Sitemap::create()
->add(Url::create('/home')
->setLastModificationDate(Carbon::yesterday())
->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
->setPriority(0.1))
->add(...)
->writeToFile($path);
A toi de voir, si tu as besoin de préciser les options de fréquence, de priorité et autres..
Url::create(route('terms.index'))
->setLastModificationDate(Carbon::yesterday())
->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
->setPriority(0)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Je ne comprends pas ta logique, stp, tu peux être plus explicite pour toutes mes routes je dois écrire le ceci " $sitemap->add(
Url::create(route('terms.index'))
->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
->setPriority(0)
);"