Adding a sitemap to your Laravel application

Submitted by msurguy - 3 years ago

I have added a sitemap to this very site and was thinking that some of you want to do the same for SEO purposes. The way to add a sitemap would be pretty easy by using a Blade template for the XML that sitemap requires but there is an easier solution. You can use a package from that would make it super simple to generate a sitemap for your Laravel application. See for yourself.

// use this package for the easy sitemap creation in Laravel 4.*:
// then, do something like this for all your dynamic and static content:

// Place the following code in a route or controller that should return a sitemap
$sitemap = App::make("sitemap");

// Add static pages like this:
$sitemap->add(URL::to('/'), '2013-11-16T12:30:00+02:00', '1.0', 'daily');
$sitemap->add(URL::to('about'), '2013-11-16T12:30:00+02:00', '0.7', 'monthly');
$sitemap->add(URL::to('categories'), '2013-11-16T12:30:00+02:00', '0.7', 'monthly');
$sitemap->add(URL::to('tags'), '2013-11-16T12:30:00+02:00', '0.7', 'monthly');
$sitemap->add(URL::to('login'), '2013-11-16T12:30:00+02:00', '0.8', 'weekly');
$sitemap->add(URL::to('register'), '2013-11-16T12:30:00+02:00', '0.8', 'weekly');

// Add dynamic pages of the site like this (using an example of this very site):

$tricks = Trick::all();

foreach($tricks as $trick) {
  $sitemap->add(URL::to("tricks/{$trick->slug}"), $trick->created_at, '0.9', 'weekly');

foreach($categories as $category) {
  $sitemap->add(URL::to("categories/{$category->slug}"), $category->created_at, '0.9', 'weekly');

// Now, output the sitemap:
return $sitemap->render('xml');
comments powered by Disqus

Ads cover the costs of maintaining Laravel-tricks