If you're working on improving your website's SEO, you've likely come across the term "sitemap." A sitemap is a file where you list all the important web pages of your site to help search engines like Google understand your website's structure. By intelligently crawling your site with this information, search engines can index your content more effectively, leading to improved visibility in search results.

In this post, you will learn how to build a custom sitemap.xml file in Laravel without relying on third-party packages.

What Is a Sitemap and Why Is It Important?

A sitemap is essentially a blueprint of your website that search engines use to navigate and understand your content. It can include URLs for web pages, blog posts, categories, or even custom resources. A basic sitemap file looks like this:


<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>https://jcadima.dev/</loc>
    </url>
    <url>
        <loc>https://jcadima.dev/blog</loc>
    </url>
    <url>
        <loc>https://jcadima.dev/projects</loc>
    </url>
    <url>
        <loc>https://jcadima.dev/contact</loc>
    </url>

</urlset>

Building a Sitemap in Laravel

Instead of using a package like spatie/laravel-sitemap, we'll create a custom sitemap using Laravel's built-in tools.

Step 1: Create the SiteMapController

Next, create a SitemapController to handle the sitemap generation:


php artisan make:controller SitemapController

Next, create a method called sitemap to handle the sitemap generation:


namespace App\Http\Controllers;

use App\Models\Post;
use App\Models\Page;

class SiteMapController extends Controller
{
    public function sitemap()
    {
        $posts = Post::orderBy('updated_at', 'DESC')->get();
        $pages = Page::orderBy('updated_at', 'DESC')->get();

        // Return the view as an XML response
        return response()->view('sitemap', compact('pages', 'posts'))
                         ->header('Content-Type', 'text/xml');
    }
}
  • Fetches blog posts and pages from the database.
  • Generates the sitemap content dynamically using a Blade view.

Define Your Sitemap Routes in Laravel

Create a route for your sitemap in the web.php file. This route will serve the dynamically generated sitemap:


use App\Http\Controllers\SiteMapController;

Route::get('/sitemap.xml', [SitemapController::class, 'sitemap']);

Step 3: Create the Blade View

Create a new Blade template for your sitemap 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>{{ url('/')}}</loc>
    </url>
    <url>
        https://yourdomain.com/blog</loc>
    </url>
    @foreach($pages as $page)
        <url>
            <loc>{{ url($page->slug) }}</loc>
        </url>
    @endforeach
    @foreach($posts as $post)
        <url>
            <loc>https://yourdomain.com/blog/{{ $post->slug }}</loc>
        </url>
    @endforeach
</urlset>

This Blade file defines the XML structure of the sitemap. The @foreach loop dynamically generates <url> tags for each blog post, ensuring that your sitemap stays up-to-date as new content is added.

Testing Your Sitemap

After setting up your controller and Blade file, visit https://yourdomain.com/sitemap.xml in your browser to view the generated sitemap. You should see a complete list of URLs in XML format.

Submit Your Sitemap to Search Engines

And with just no more than three steps you have a dynamic working sitemap. There are a few other options we can add to our pages and posts for example the ability to pick individual posts or pages to be visible in your sitemap.xml listing, but that will be for another blog post

Once your sitemap is live, submit it to search engines like Google using their respective tools

For more details, check out Google's official sitemap guide.