Generate dynamic sitemap in Laravel

In the world of SEO, sitemap is a very important element that makes a website searchable on the internet. Sitemap tells the search engine the roadmap of all the pages of the website. This improves the indexing and visibility of our website. Dynamic sitemap of any website is very important, because the sitemap of static pages is generated only once, but it is very important to automatically add those pages which are created dynamically in the sitemap. In this article, we will learn about Generate dynamic sitemap in Laravel with the help of examples.

What is a Dynamic Sitemap?

When we generate sitemap of all pages of website programmatically, we call it Dynamic Sitemap. We generate static sitemap only once, and whenever we make any change, then we manually update those pages info in sitemap. But in dynamic sitemap everything gets updated automatically, like new blog published, product updates, or generate dynamic pages etc. It means we do not need to manually update sitemap for adding new content or removing any content.

To create a dynamic sitemap in Laravel we need basic knowledge of Controller, Eloquent, Routes and View. Now we will see the step by step process of generating a dynamic sitemap.

Suppose, in our website there are two tables named Category and Post for which we need to generate the sitemap.

Step 1: First we will declare two routes in routes.php for post and category

<?php

Route::get('post.xml', 'SitemapController@sitemap_post');

Route::get('category.xml', 'SitemapController@sitemap_category');

Note: Here we are using Laravel 9. Declare routes based on the version of Laravel you are using.

Step 2: Now we will create a SitemapController. We will create this controller with the help of Artisan Commands.

php artisan make:controller SitemapController

After the SitemapController is created, we will add below content

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Category;

use App\Models\Posts;

class SitemapController extends Controller
{

    public function sitemap_post()
    {

        $posts = Posts::whereNull('deleted_at')->get();

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

    public function sitemap_category()
    {
        $category = Category::whereNull('deleted_at')->get();

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

}

 

Step 3: Now we will create a folder named sitemap in views folder. And we will create two blade files sitmap_post.blade.php & sitmap_category.blade.php.

sitemap_post.blade.php

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

@foreach($posts AS $postlist)   

<url>
    <loc>{{ env('APP_URL').'/'.$postlist->slug }}</loc>
    <lastmod>{{ gmdate('Y-m-d\TH:i:s\Z',strtotime($postlist->updated_at)) }}</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.65</priority>
    
</url>

@endforeach

</urlset>

sitemap_category.blade.php

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

@foreach($category AS $categorylist)   

<url>
    <loc>{{ env('APP_URL').'/category/'.$categorylist->slug }}</loc>
    <lastmod>{{ gmdate('Y-m-d\TH:i:s\Z',strtotime($categorylist->updated_at)) }}</lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.65</priority>
    
</url>

@endforeach

</urlset>

Here changefreq is different according to Google. And in priority we assign number in the range of 0 - 1.

Changefreq Guidelines with Example

  • Never: This is mostly used for old news, press releases etc.
  • Yearly: This is mostly used for the contact us, about us, login, registration etc. pages of the website. Because these do not change frequently.
  • Monthly: Its mostly used for FAQs, articles pages etc.
  • Weekly: This is mostly used for product information pages.
  • Daily: Mostly blogs entries are used for this.
  • Hourly: This is mostly done by news websites, forums etc.
  • Always: This is mostly used for stock market data.

Priiority Guideline

  • 0.8 - 1.0: Mostly used for Homepage, product information, category pages
  • 0.4 - 0.7: Mostly used for blogs, posts, pages, FAQs etc
  • 0.0 - 0.3: Mostly used for outdated news 

Step 4: Now we will test both the sitemaps on the browser. For this we will first start the Laravel server

php artisan serve

Now we will check it by opening the sitemap on the browser

http://localhost:8000/post.xml
http://localhost:8000/category.xml

After checking the sitemap, if all the links are generated correctly, then we submit both these sitemaps to Google Search Console.

Tips for Better Sitemap

  • If our website has lots of URLs, we should compile them in multiple systems.
  • We should add links only to those pages which we want to be indexed in search engines.
  • If we are using Laravel to generate a sitemap, we should use route caching for speed by using this command php artisan route:cache

Conclusion

If you have the correct tools, creating a dynamic sitemap in Laravel is surprisingly easy. Regardless of whether you're running a blog, product catalog, or content-heavy platform, automating the creation of sitemaps guarantees that your SEO strategy remains effective without requiring manual labor.

 

Top