Laravel — create a simple sitemap.xml

sitemap-generator

Create a Sitemap Controller

php artisan make:controller SitemapController

Open SitemapController.php to generate our index. For this example I used Post model:

public function index()
{
  $posts = Post::orderBy('updated_at', 'desc')->get();

  return response()->view('sitemap', [
      'posts' => $posts,
  ])->header('Content-Type', 'text/xml');
}


Create a blade template for sitemap:

'; ?>

xmlns:xhtml="http://www.w3.org/1999/xhtml">
@foreach ($posts as $post)

https://limitless.pro/blog/{{ $post->slug }}
{{ $post->published_at->tz('UTC')->toAtomString() }}
weekly
0.8

@endforeach


Add routes

Route::get('/sitemap', 'SitemapController@index');
Route::get('/sitemap.xml', 'SitemapController@index');

Done.