Laravel Tutorials - How to Generate a Simple XML Sitemap using Laravel

How to Generate a Simple XML Sitemap using Laravel

2020-03-08 Lukas Markevičius
How to Generate a Simple XML Sitemap using Laravel featured image

Today we are going to learn how to generate a simple XML sitemap using Laravel. Mainly you can create it either manually or create some sort of automated solution. To create it manually you can make your own sitemap.xml file and map your website's URLs or you can use online sitemap generators where you enter URL of your website and it generates sitemap where later you can upload it to your server. But this method is only good if you have a static website and your structure isn't changing so frequently. But what if you have a blog or a product website where your pages are generated dynamically? Then you would need to generate a sitemap almost daily and re-upload it to your server. That would take a lot of your time! So today we are going to learn how to create a dynamic XML sitemap. You will learn:

What is a sitemap?

A sitemap is a file where you can list the web pages of your site to tell Google and other search engines about the organization of your site content. Search engine web crawlers like Googlebot read this file to more intelligently crawl your site. Also, your sitemap can provide valuable metadata associated with the pages you list in that sitemap: Metadata is information about a webpage, such as when the page was last updated, how often the page is changed, and the importance of the page relative to other URLs in the site.

Structure of XML sitemap

Sitemap files must be UTF-8 encoded and start and end with <urlset> </urlset> tags.

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> 

</urlset>

Every website's URL has to start with <url> and <loc> tags.

<url>
    <loc>http://www.example.com/foo.html</loc> 
</url>

You can also specify other tags which can help crawlers to find more details but are not required.

Generating dynamic XML Sitemap

So, after we learned a little bit about what is a sitemap we can start generating our own. Let's say we have the following pages:

  • Home page: https://example.com/
  • Blog index page: https://example.com/blog
  • About Me page: https://example.com/about-me
  • Contact page: https://example.com/contact

And we also have blog posts which have unique slugs, so to create dynamically posts URL we could use foreach function. So, let's define a blade view in resources/views/sitemap.blade.php:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>https://example.com/</loc>
    </url>
    <url>
        <loc>https://example.com/blog</loc>
    </url>
    <url>
        <loc>https://example.com/about-me</loc>
    </url>
    <url>
        <loc>https://example.com/contact</loc>
    </url>
    @foreach($posts as $post)
        <url>
            <loc>https://example.com/blog/{{ $post->slug }}</loc>
        </url>
    @endforeach
</urlset>

Now you can create a new controller or use an existing one to collect all the posts from the database and return them to the sitemap.blade.php view:

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

    return response()->view('sitemap', compact('posts'))->header('Content-Type', 'text/xml');
}

Finally, we need to define a route in routes/web.php:

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

and that's it! You now have a dynamically generated XML sitemap. And whenever you publish a new post it will be automatically added to your sitemap.

How to Generate a Simple XML Sitemap using Laravel pinterest image

Related Tutorials

How to Create Slugs in Laravel

Posted 2020-03-27 Lukas Markevičius

Today we are going to learn how to create slugs in Laravel. You are going to learn: Why use slugs How to create slugs How to show objects with the slugs How to edit slugs Why use Slugs If yo...

How to create Step by Step Laravel Project

Posted 2020-03-08 Lukas Markevičius

If you are a beginner and want to learn how to start developing Laravel applications you came to the right place because today we are going to learn step-by-step how to create your first Laravel 5.6 p...

Back