Duane Blake

Front end developer

Creating XML sitemaps in Laravel

In a world where Penguin and Panda can cripple your organic traffic to your site. Certain things never change adding an XML sitemap is one of the core things I always add to my websites and registering google webmaster tools.

Xml Sitemap in Laravel

I’m going to be using a great package called laravel-sitemap by RoumenDamianoff which generates the sitemap for the website.

Before I start showing how to use the package. I will briefly show you what goes in an xml sitemap. What goes in each of the sitemap sections.


 <url>
    <loc>http://www.duaneblake.co.uk/</loc>
    <lastmod>2014-10-09T07:00:50+00:00</lastmod>
    <changefreq>weekly</changefreq>
    <priority>1</priority>
</url>
Tag Description
<url> This is where the information for the url is stated
<loc> The url for the page
<lastmod> This is the last modified date of when the page was last changed
This is the frequency of the page changes –

always. Use for pages that change every time they are accessed.
hourly
daily
weekly
monthly
yearly
never

<priority> This is the priority of how Google views the page

As with most things with Google this isn’t the bullet proof guide to get the pages indexed but its more of a gentle nudge to tell google where to look.

I now describe the process of integrating the laravel-sitemap into your application

  1. Add the package into your composer file.

    
    "require": {
       "laravel/framework": "4.2.*",
        "roumen/sitemap": "dev-master"
    },
    

    Then run a composer update from command line

  2. Now add in the provider into your app/config.
    
    'Roumen\Sitemap\SitemapServiceProvider',
    
  3. Now create a controller called sitemapController or place it in an existing controller
  4. Add the sitemap into the routes file
    Route::get('/sitemap', 'siteMapController@sitemap');
  5. Create a function called sitemap, The example below is from my site Motivational Monday the site does a motivational quote every Monday.
    
    public function sitemap() {
      // This creates the sitemap
      $sitemap = App::make("sitemap");
    
      // I Use Carbon to create a date which says the next Monday
        $lastMonday = new Carbon('last monday');
      
      //Sets the sitemap to last monday as my site is always updated on Monday
      $sitemap->add(URL::to('/'), $lastMonday, 1, 'weekly');
      $sitemap->add(URL::to('all'), $lastMonday, '0.8', 'weekly');
                
      // This fetches all the current quotes which are live on the site and assign it to the $quotes
          $quotes = Quote::liveQuotes()->get();
                
      // I add the url to the site, I set the published date so when the URL last got updated, I set the priority as 0.4 and then tell the sitemap that the site get updated yearly.
           foreach ($quotes as $quote) {
              $sitemap->add(route('quotes.single', $quote->id),
              $quote->published_date, '0.4', 'yearly');
           }
           return $sitemap->render('xml');
    

The sitemap now been generated on the following url /sitemap

Leave a comment

Your email address will not be published. Required fields are marked *