Skip to main content

Sitemap

Here is a clear and structured documentation page for your GenerateSitemap command within Peak, extending it to include usage, customization, and scheduling best practices:

Development Guide: Generating Sitemaps​

Overview​

Peak provides a built-in console command to generate a sitemap.xml file for your application. This file helps search engines like Google and Bing better understand the structure of your website and improves indexing.

The command supports multiple dynamic models such as Post, Page, Doc, and Roadmap, and generates a fully valid XML sitemap saved in your public/ directory.

Command Usage​

Command Signature​

php artisan sitemap:generate

Description​

This command will:

  1. Create a new sitemap instance.
  2. Use Spatie’s SitemapGenerator to crawl your site and collect base URLs.
  3. Loop through a list of defined models and add each model’s URLs to the sitemap (must implement Spatie\Sitemap\Tags\Url via the Sitemapable interface or similar logic).
  4. Save the resulting sitemap as public/sitemap.xml.

Example Code​

Below is the implementation of the sitemap:generate command:

protected $signature = 'sitemap:generate';
protected $description = 'Generate sitemap.xml';
public function handle()
{
$this->info('Generating sitemap...');

try {
$sitemap = Sitemap::create();
$models = [
Post::class,
Page::class,
Doc::class,
Roadmap::class,
];

$path = public_path('sitemap.xml');

// Crawl site to include static routes
SitemapGenerator::create(config('app.url'))->writeToFile($path);

// Add dynamic content from models
foreach ($models as $model) {
$this->info("Adding entries for: " . class_basename($model));

foreach ($model::query()->cursor() as $item) {
$sitemap->add($item); // Assumes each model is sitemap-compatible
}
}

$sitemap->writeToFile($path);

$this->info("Sitemap generated successfully at: {$path}");
} catch (\Exception $e) {
$this->error('Failed to generate sitemap: ' . $e->getMessage());
}
}

Model Requirements​

Each model you want to include in the sitemap must be able to return a valid URL. You can achieve this by either:

  • Implementing Spatie's Sitemapable interface.
  • Defining a getSitemapUrl() method or ensuring the model returns a valid Spatie\Sitemap\Tags\Url object in the loop.

Example:

public function toSitemapTag(): \Spatie\Sitemap\Tags\Url
{
return \Spatie\Sitemap\Tags\Url::create(route('posts.show', $this));
}

Scheduling the Command​

To ensure your sitemap stays up-to-date, it's recommended to automate its generation using Laravel's scheduler.

Step 1: Add to App\Console\Kernel.php​

protected function schedule(Schedule $schedule)
{
$schedule->command('sitemap:generate')->dailyAt('02:00');
}

Best Practice​

  • Schedule Frequency: Once daily is usually sufficient, unless your site updates very frequently.
  • Optimal Time: Run the task during low-traffic hours (e.g., 2:00 AM) to reduce potential performance impact.

Output Location​

After successful generation, the sitemap is saved here:

public/sitemap.xml

Make sure your web server allows access to this file at:

https://your-domain.com/sitemap.xml

Summary​

| Feature | Description | | - | | | Command name | sitemap:generate | | Purpose | Generate and update your sitemap with dynamic and static URLs | | Default output path | public/sitemap.xml | | Includes | Crawled URLs + model entries (Post, Page, Doc, Roadmap) | | Scheduling recommendation | Daily at 02:00 AM via Laravel Scheduler |

Let me know if you also want to document how to test the sitemap file or ping search engines after generation.