[Laravel] 서치엔진용 xml 만들기
라라벨을 이용한 서치엔진용 xml 만들기
사이트맵을 위한 컨트롤러 생성
php artisan make:controller SitemapController
app/Http/Controllers/SitemapController
public function index()
{
$post = Post::active()->orderBy('updated_at', 'desc')->first();
return response()->view('sitemap', [
'post' => $post
])->header('Content-Type', 'text/xml');
}
sitemap.blade.php
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
@foreach ($posts as $post)
<url>
<loc>https://laravel-news.com/{{ $post->uri }}</loc>
<lastmod>{{ $post->publishes_at->tz('UTC')->toAtomString() }}</lastmod>
<changefreq>weekly</changefreq>
<priority>0.6</priority>
</url>
@endforeach
</urlset>