Surfe.be - passive income

How to Create Dynamic XML Sitemap in Laravel

What is a sitemap?

A sitemap is a file where you provide information about the pages, videos, and other files on your site, and the relationships between them. Search engines like Google read this file to more intelligently crawl your site. A sitemap tells the crawler which files you think are important in your site, and also provides valuable information about these files: for example, for pages, when the page was last updated, how often the page is changed, and any alternate language versions of a page.

You can use a sitemap to provide information about specific types of content on your pages, including video and image content. For example:

  • 1. A sitemap video entry can specify the video running time, category, and age appropriateness rating.
  • 2. A sitemap image entry can include the image subject matter, type, and license.

Learn more from here Do i need a sitemap?

Build and submit a sitemap

We can generate sitemap from here but we need to create sitemap using laravel by our own coding style. Go to this link to learn more about sitemap and the rules of sitemap . When we submit our sitemap in our google web master without creating sitemap then its show us 404 error.

general http error in sitemap

So removing this error first we have to create our using coding or will create from here . But in this tutorial we will see how to create a xml sitemap using Laravel.

Example of a sitemap :

<?xml version="1.0" encoding="UTF-8"?>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

   <url>

      <loc>http://www.example.com/</loc>

      <lastmod>20018-01-01</lastmod>

      <changefreq>monthly</changefreq>

      <priority>0.9</priority>

   </url>

</urlset>

Benefits of having more than one sitemap

Let's say we have database tables for ArticlesCategories and Tags. We can create seperate xml sitemaps for each of them which will be easily managable and also be very clear to read for both humans and search engine robots. Then we will include all those 4 sitemaps into one index file and submit to google, bing or wherever we may please.

Getting Started

Let's create a new controller for our sitemaps.

php artisan make:controller SitemapController

Let's create a index method inside SitemapController class that will generate all the xml sitemaps we need. We will create 3 sitemapes for 3 of our database tables which are ArticlesCategories and Tags. All of them will be included in one single sitemap index.

public function index()
    {
      $articles = Article::all()->first();
      $categories = Category::all()->first();
      $tags = Tag::all()->first();

      return response()->view('sitemap.index', [
          'articles' => $articles,
          'categories' => $categories,
          'tags' => $tags,
      ])->header('Content-Type', 'text/xml');
    }

Here we returned a response object to the view, and set the text/xml header to make sure the header is available to the view, we have includeed the response first. Make sure you have called the models on top of your class .

sitemap view

Go ahead and create sitemap folder in your laravel application resources/views/sitemap and create a file index.blade.php. We are going to wrap 4 of our <sitemap> inside <sitemapindex></sitemapindex> using <loc>. This is how our index page must look like:

Replace http://127.0.0.1:8000  with your domain name such as mywebsite.com

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>

<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <sitemap>
        <loc>http://127.0.0.1:8000/sitemap.xml/articles</loc>
    </sitemap>
    <sitemap>
        <loc>http://127.0.0.1:8000/sitemap.xml/categories</loc>
    </sitemap>
    <sitemap>
        <loc>http://127.0.0.1:8000/sitemap.xml/tags</loc>
    </sitemap>

</sitemapindex>

Dynamic url's for the sitemap

Head over to SitemapController class and create more methods for each of the Database Tables that we want to generate url's to include in the sitemap. Here I have created methods for articlescategoriesquestions and tags.

 public function articles()
    {
        $articles = Article::latest()->get();
        return response()->view('sitemap.articles', [
            'articles' => $articles,
        ])->header('Content-Type', 'text/xml');
    }

    public function categories()
    {
        $categories = Category::all();
        return response()->view('sitemap.categories', [
            'categories' => $categories,
        ])->header('Content-Type', 'text/xml');
    }

    public function tags()
    {
        $tags = Tag::all();
        return response()->view('sitemap.tags', [
            'tags' => $tags,
        ])->header('Content-Type', 'text/xml');
    }

Below is how my articles.blade.php looks like. I have put my changfreq as weekly which can be changed to dailymonthly or yearlytoo. You can also set the priority which ranges from 0.1 to 1.0. you can read more about it in sitemaps protocal page. I just shoed here in article page , you need to print all those pages like categories.blade.php, tags.blade.php also.

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    @foreach ($articles as $article)
        <url>
            <loc>http://127.0.0.1:8000/articles/{{ $article->slug }}</loc>
            <lastmod>{{ $article->created_at->tz('UTC')->toAtomString() }}</lastmod>
            <changefreq>weekly</changefreq>
            <priority>0.9</priority>
        </url>
    @endforeach
</urlset>

Please keep in mind that <lastmod><changefreq> and <priority> tags are optional. So feel free to avoid them if you like.

Creating routes for the sitemap

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

Now I can go to http://127.0.0.1:8000/sitemap.xml to see the list of sitemaps that I created. If you go to your website.com/sitemap.xml, You should also see the list of sitemaps you created like the screenshot below.

Now go to those slug to see all the sitemap like  http://127.0.0.1:8000/sitemap.xml/articles or http://127.0.0.1:8000/sitemap.xml/categories  to see the list of sitemaps. That’s it. Hope you will like this tutorial, if you face any error please leave a comments & don’t forget to share.

Leave a comments

LET'S SOCIALITE

Recent Tweets

RECOMMENDED POST