Laravel 8 Sitemap Generator Package Tutorial

Share this Article

Sitemap helps to enhance the url crawl for a website. It lists all the links at a single place. It is a method to rank urls to search engines. Inside this article we will see the concept of Laravel 8 sitemap generator package.

How to generate automatic sitemap for website, we will see in a very easy and detailed way. We will list all urls into XML format. We will use a composer package for this task.

Generating sitemap xml format in a dynamic way in laravel 8.

Let’s get started.


Installation of Laravel 8 Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project. If your system doesn’t has Composer Installed, Click here to Install Composer ? Here is the complete command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

After following these steps we can install a Laravel 8 application into system. To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel 8 already installed at system.


Installation of Spatie Laravel Package

Open project into terminal and run this composer command.

$ composer require spatie/laravel-sitemap

Package Publish & Configuration

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

Usage of Spatie Sitemap Package

Syntax

SitemapGenerator::create('SITE-URL')->writeToFile("FILENAME-TO-WRITE");

Example

We will create a closure route in web.php. Open web.php from /routes folder.

<?php

use Illuminate\Support\Facades\Route;
use Spatie\Sitemap\SitemapGenerator;

Route::get("generate-sitemap", function () {

    SitemapGenerator::create('https://onlinewebtutorblog.com/')->writeToFile(public_path('sitemap.xml'));
    dd("done");
});
  • When we run this, it will create a file with the name sitemap.xml file inside /public folder which contains the urls of given website.
  • To generate sitemap.xml file for itself, we need to pass app url, Use config(‘app.url’)
SitemapGenerator::create(config('app.url'))->writeToFile(public_path('sitemap.xml'));

If we open sitemp.xml file, we will see the list of urls. In this case, it will be something –

URL – http://127.0.0.1:8001/sitemap.xml

But every time, we need to run the route manually like http://127.0.0.1:8000/generate-sitemap to generate or update sitemap.xml file.

To automate this process, we need to use laravel 8 command method. We have to create scheduled cron job which runs this same url but at a specific time period.


Create Artisan Command

Open terminal and run this artisan command.

$ php artisan make:command GenerateSitemap

It will create a file GenerateSitemap.php at /app/Console/Commands folder.

Open GenerateSitemap.php and write this complete code into it.

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Spatie\Sitemap\SitemapGenerator;

class GenerateSitemap extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sitemap:generate';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'This command will generate sitemap.xml file for your site';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        SitemapGenerator::create(config('app.url'))
            ->writeToFile(public_path('sitemap.xml'));
    }
}

Open terminal and run this command

$ php artisan

This command will open the command pallet of laravel artisan, we should see now the created custom command.

Run Artisan Command

To run this created artisan command. Back to terminal

$ php artisan sitemap:generate

It will create sitemap.xml to your /public folder.


Schedule Laravel Command

To automate this process, we need to set a specific time interval to run. Open Kernel.php from /app/Console folder.

Find schedule() method from that and add this given line into it.

/**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
*/
protected function schedule(Schedule $schedule)
{
  // $schedule->command('inspire')->hourly();
  $schedule->command('sitemap:generate')->daily();
}

This is very simple, then your sitemap.xml should be available at site_url/sitemap.xml

We hope this article helped you to learn about Laravel 8 Sitemap Generator Package Tutorial in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on Laravel 8 Articles here

Leave a Comment

What are CSRF Functions in CodeIgniter 4 Tutorial
MySQL Stored Procedure in CodeIgniter 4 Tutorial