Blog Detail

02

Sep
Automatically Generate sitemaps in Laravel for SEO cover image

arrow_back Automatically Generate sitemaps in Laravel for SEO

A sitemap is a blueprint of your website that assists search engines to find, crawl, and index the content of your website. Sitemaps also inform search engines which pages on your site are most significant. A sitemap is very crucial for your website because search engines like Google, Yahoo, and Bing use your sitemap to find different pages on your site. There are many sitemap packages through which you can easily generate a sitemap for your website, they all have in common that you have to manually add links that must appear in the sitemap. But Spatie has launched a package that can automatically build up a sitemap by crawling a site. This package can generate a sitemap without you having to add URLs to it manually. This works by crawling your entire site.

Installation

First, you’ve to install this package via composer:

composer require spatie/laravel-sitemap

This package will automatically register itself.
If you want to update your sitemap automatically and frequently you need to perform some extra steps.

Configuration

You can override the default options for the crawler. First publish the configuration:

php artisan vendor:publish --provider="Spatie\Sitemap\SitemapServiceProvider" --tag=config

This will copy the default config to config/sitemap.php where you can edit it.

Usage

Generating a sitemap

The easiest way is to crawl the given domain and generate a sitemap with all found links. The destination of the sitemap should be specified by $path.

use Spatie\Sitemap\SitemapGenerator;
SitemapGenerator::create('https://example.com')->writeToFile($path);

The generated sitemap will look similar to this:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>https://example.com</loc>
        <lastmod>2016-01-01T00:00:00+00:00</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>
    <url>
        <loc>https://example.com/page</loc>
        <lastmod>2016-01-01T00:00:00+00:00</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>
    ...
</urlset>

You can also create your sitemap manually:

use Carbon\Carbon;
use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\Tags\Url;
Sitemap::create()
    ->add(Url::create('/home')
        ->setLastModificationDate(Carbon::yesterday())
        ->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
        ->setPriority(0.1))
   ->add(...)
   ->writeToFile($path);

Or you can have the best of both worlds by generating a sitemap and then adding more links to it:

SitemapGenerator::create('https://example.com')
   ->getSitemap()
   ->add(Url::create('/extra-page')
        ->setLastModificationDate(Carbon::yesterday())
        ->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
        ->setPriority(0.1))
    ->add(...)
    ->writeToFile($path);

You can also control the maximum depth of the sitemap:

SitemapGenerator::create('https://example.com')
    ->configureCrawler(function (Crawler $crawler) {
        $crawler->setMaximumDepth(3);
    })
    ->writeToFile($path);

The generator has the ability to execute JavaScript on each page, so links are injected into the dom by JavaScript will be crawled as well.

You can also use one of your available filesystem disks to write the sitemap.

SitemapGenerator::create('https://example.com')->getSitemap()->writeToDisk('public', 'sitemap.xml');

You can also add your models directly by implementing the \Spatie\Sitemap\Contracts\Sitemapable interface.

use Spatie\Sitemap\Contracts\Sitemapable;
use Spatie\Sitemap\Tags\Url;
class Post extends Model implements Sitemapable
{
    public function toSitemapTag(): Url | string | array
    {
        return route('blog.post.show', $this);
    }
}

Now you can add a single post model to the sitemap or even a whole collection.

use Spatie\Sitemap\Sitemap;
Sitemap::create()
    ->add($post)
    ->add(Post::all());

This way you can add all your pages super-fast without the need to crawl them all.

This package has a lot of options in it. If you want to utilize this package, You can visit its whole documentation on Github.

Published at : 02-09-2021

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project