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:
- Create a new sitemap instance.
- Use Spatieβs
SitemapGenerator
to crawl your site and collect base URLs. - Loop through a list of defined models and add each modelβs URLs to the sitemap (must implement
Spatie\Sitemap\Tags\Url
via theSitemapable
interface or similar logic). - 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 validSpatie\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.