Selam ben laravelde sitemap için spatie sitemap paketini kullanayım dedim chat gptye sordum uzun süreli kod iyileştirmesi sonrası şöyle kodlar aldım sizce hatalı bir tarafı var mıdır ?
Ayrıca sorum şudur ki ben rotue ve app/Console/Commands/UpdateSitemap.php da aynı şeyleri yazmalı mıyım ?
Sunucuya atıcam o yüzden sorayım dedim cron falanda ekleyeceğim.
Route
Route::get('/sitemap.xml', function () {
$sitemapPath = 'sitemap.xml';
// Check if sitemap.xml already exists in Redis cache
if (Redis::exists($sitemapPath)) {
// If it exists in Redis cache, serve it with headers
$file = Redis::get($sitemapPath);
$headers = [
'Content-Type' => 'application/xml',
'Content-Encoding' => 'gzip',
];
return Response::make($file, 200, $headers)
->header('Cache-Control', 'public, max-age=3600');
}
// If it doesn't exist in Redis cache, generate the sitemap and store it
$sitemap = Sitemap::create();
// Add URLs to the sitemap
$posts = Post::all();
foreach ($posts as $post) {
$lastModifiedDate = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $post->updated_at);
$sitemap->add(
Url::create(route('post.show', $post->slug))
->setLastModificationDate($lastModifiedDate)
->setChangeFrequency('monthly')
->setPriority(0.9)
);
}
// Write the sitemap to a file
$sitemap->writeToFile($sitemapPath);
// Get the file contents and compress them with gzip
$fileContents = Storage::disk('local')->get($sitemapPath);
$gzipContents = gzencode($fileContents);
// Store the compressed file in Redis cache
Redis::set($sitemapPath, $gzipContents);
Redis::expire($sitemapPath, 3600);
// Serve the sitemap with headers
$headers = [
'Content-Type' => 'application/xml',
'Content-Encoding' => 'gzip',
];
// Sitemap.xml dosyasına bir stil bilgisi ekleyin
$stylesheet = '<?xml-stylesheet type="text/xsl" href="' . asset('sitemap.xsl') . '"?>';
$file = str_replace('<?xml version="1.0"?>', '<?xml version="1.0"?>' . $stylesheet, $file);
return Response::make($gzipContents, 200, $headers)
->header('Cache-Control', 'public, max-age=3600');
})->name('sitemap');
app/Console/Commands/UpdateSitemap.php
protected $signature = 'sitemap:update';
protected $description = 'Update sitemap';
public function handle()
{
$sitemapPath = 'sitemap.xml';
// Check if sitemap.xml already exists in Redis cache
if (!Redis::exists($sitemapPath)) {
$sitemap = Sitemap::create();
$sitemap->add(
Url::create('/')
->setLastModificationDate(Carbon::now())
->setChangeFrequency('monthly')
->setPriority(0.8)
);
$posts = Post::all();
foreach ($posts as $post) {
$lastModifiedDate = Carbon::createFromFormat('Y-m-d H:i:s', $post->updated_at);
$sitemap->add(
Url::create('/post/' . $post->slug)
->setLastModificationDate($lastModifiedDate)
->setChangeFrequency('monthly')
->setPriority(0.9)
);
}
// Add the 'about' page to the sitemap
$sitemap->add(
Url::create('/about')
->setLastModificationDate(Carbon::now())
->setChangeFrequency('monthly')
->setPriority(0.8)
);
// Add the 'contact' page to the sitemap
$sitemap->add(
Url::create('/contact')
->setLastModificationDate(Carbon::now())
->setChangeFrequency('monthly')
->setPriority(0.8)
);
// Add the 'terms' page to the sitemap
$sitemap->add(
Url::create('/terms')
->setLastModificationDate(Carbon::now())
->setChangeFrequency('monthly')
->setPriority(0.8)
);
$sitemapContent = $sitemap->render();
// Compress the sitemap.xml content with gzip
$gzipContent = gzencode($sitemapContent);
// Save the compressed sitemap.xml content to Redis cache
Redis::set($sitemapPath, $gzipContent);
$this->info('Sitemap updated successfully!');
} else {
$this->info('Sitemap is already up to date!');
}
// Serve the compressed sitemap file with caching headers
return Response::make(gzdecode(Redis::get($sitemapPath)), 200, [
'Content-Type' => 'application/xml',
'Content-Encoding' => 'gzip',
'Cache-Control' => 'public, max-age=3600',
]);
}
app/Console/Kernel.php
protected $commands = [
Commands\UpdateSitemap::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('sitemap:update')->hourly();
}