Doc Suggestion: Generating Large Sitemaps Through a Command
Created by: dingman
I made some modifications to the documented example for large sitemaps and wanted to share. I used 10,000 instead of 50,000 because my VM couldn't handle 50,000 in one shot. Certainly welcome to feedback on it, I'm a novice developer.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class GenerateSitemaps extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'generate:sitemap';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate XML sitemaps';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$start = microtime(true);
// generate new sitemap file
$sitemap = \App::make('sitemap');
$counter = 0;
$sitemapCounter = 0;
\App\Product::with('details')
->orderBy('id','asc')
->chunk(10000, function ($products) use (&$sitemap, &$counter, &$sitemapCounter) {
foreach ($products as $p) {
if ($counter == 10000) {
// generate new sitemap file
$sitemap->store('xml', 'sitemap-' . $sitemapCounter);
// add the file to the sitemaps array
$sitemap->addSitemap(secure_url('sitemap-' . $sitemapCounter . '.xml'));
// reset items array (clear memory)
$sitemap->model->resetItems();
// reset the counter
$counter = 0;
// count generated sitemap
$sitemapCounter++;
}
// add product to items array
$p->freq = 'daily';
$p->priority = '0.9';
$p->timestamp = $p->details->pluck('updated_at')->sortByDesc('updated_at')->first();
$p->permalink = route('product', $p->slug);
$sitemap->add($p->permalink, $p->timestampe, $p->priority, $p->freq);
// count number of elements
$counter++;
}
});
// you need to check for unused items
if (!empty($sitemap->model->getItems())) {
// generate sitemap with last items
$sitemap->store('xml', 'sitemap-' . $sitemapCounter);
// add sitemap to sitemaps array
$sitemap->addSitemap(secure_url('sitemap-' . $sitemapCounter . '.xml'));
// reset items array
$sitemap->model->resetItems();
}
// generate new sitemapindex that will contain all generated sitemaps above
$sitemap->store('sitemapindex', 'sitemap') ;
$end = microtime(true);
$this->info("Took ".($end - $start) ." seconds to complete\n");
}
}