Files
memefast/app/Console/Commands/GenerateStaticSitemap.php
2025-07-17 03:42:55 +08:00

81 lines
2.6 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\Tags\Url;
class GenerateStaticSitemap extends Command
{
protected $signature = 'sitemap:generate:static';
protected $description = 'Generate sitemap for static pages (home, privacy, terms, dmca, meme library)';
public function handle(): int
{
$this->info('Starting static sitemap generation...');
$sitemap = Sitemap::create();
// Get the base URL from config
$baseUrl = config('app.url');
// Add static pages
$staticPages = [
[
'url' => $baseUrl,
'priority' => 1.0,
'changeFrequency' => Url::CHANGE_FREQUENCY_WEEKLY,
'lastModificationDate' => now(),
],
[
'url' => $baseUrl.'/meme-library',
'priority' => 0.9,
'changeFrequency' => Url::CHANGE_FREQUENCY_DAILY,
'lastModificationDate' => now(),
],
[
'url' => $baseUrl.'/privacy',
'priority' => 0.3,
'changeFrequency' => Url::CHANGE_FREQUENCY_YEARLY,
'lastModificationDate' => now()->subMonths(6), // Adjust based on when you last updated
],
[
'url' => $baseUrl.'/terms',
'priority' => 0.3,
'changeFrequency' => Url::CHANGE_FREQUENCY_YEARLY,
'lastModificationDate' => now()->subMonths(6), // Adjust based on when you last updated
],
[
'url' => $baseUrl.'/dmca-copyright',
'priority' => 0.2,
'changeFrequency' => Url::CHANGE_FREQUENCY_YEARLY,
'lastModificationDate' => now()->subMonths(6), // Adjust based on when you last updated
],
];
// Add each static page to the sitemap
foreach ($staticPages as $page) {
$url = Url::create($page['url'])
->setPriority($page['priority'])
->setChangeFrequency($page['changeFrequency'])
->setLastModificationDate($page['lastModificationDate']);
$sitemap->add($url);
$this->info("Added: {$page['url']}");
}
// Save the sitemap
$sitemapPath = public_path('sitemap_static.xml');
$sitemap->writeToFile($sitemapPath);
$this->info('Static sitemap generated successfully!');
$this->info("Sitemap saved to: {$sitemapPath}");
$this->info('Total URLs: '.count($staticPages));
return Command::SUCCESS;
}
}