To achieve dynamic sitemaps for different areas of your CMS using the spatie/laravel-sitemap package, you can follow these steps:
-
Create Individual Sitemap Generation Commands:
Create separate Artisan commands for generating each type of sitemap (pages, posts, events, etc.). This will allow you to modularize the sitemap generation process.
For example, create a command for generating the pages sitemap:
php artisan make:command GeneratePagesSitemapIn the
handlemethod of this command, use thespatie/laravel-sitemappackage to generate the sitemap:use Spatie\Sitemap\Sitemap; use Spatie\Sitemap\Tags\Url; public function handle() { Sitemap::create() ->add(Url::create('/page1')) ->add(Url::create('/page2')) // Add more pages dynamically ->writeToFile(public_path('sitemap-pages.xml')); }Repeat this process for posts, events, etc.
-
Create a Command to Generate the Sitemap Index:
Create another command to generate the sitemap index file, which will reference the individual sitemaps.
php artisan make:command GenerateSitemapIndexIn the
handlemethod, generate the sitemap index:use Spatie\Sitemap\SitemapIndex; public function handle() { SitemapIndex::create() ->add('/sitemap-pages.xml') ->add('/sitemap-posts.xml') ->add('/sitemap-events.xml') // Add more sitemaps as needed ->writeToFile(public_path('sitemap.xml')); } -
Set Up a Scheduled Task:
In your
App\Console\Kernel.php, schedule the commands to run in sequence. This ensures that each sitemap is generated before the index is created.protected function schedule(Schedule $schedule) { $schedule->command('sitemap:generate-pages')->dailyAt('00:00'); $schedule->command('sitemap:generate-posts')->dailyAt('00:05'); $schedule->command('sitemap:generate-events')->dailyAt('00:10'); $schedule->command('sitemap:generate-index')->dailyAt('00:15'); }Adjust the timing as necessary to ensure each command has enough time to complete before the next one starts.
-
Consider Using Queues:
If the sitemap generation process is resource-intensive, consider dispatching each command as a queued job. This can help manage server load and ensure that each task completes successfully.
Modify each command to dispatch a job:
use Illuminate\Support\Facades\Bus; public function handle() { Bus::chain([ new GeneratePagesSitemapJob(), new GeneratePostsSitemapJob(), new GenerateEventsSitemapJob(), new GenerateSitemapIndexJob(), ])->dispatch(); }
By following these steps, you can dynamically generate sitemaps for different areas of your CMS and ensure that the sitemap index is created only after all individual sitemaps are generated.