GENERATE SITEMAP WITH LARAVEL

Sitemap is very important for our website.

Sitemap is a file where you can list pages of your website to tell search engines about of your site content.

Search engine(SEO) fetch this file to crawl your website.

We can create sitemap with laravel, but we need one package.


Step 1 : Install package

Go to your project root in terminal & run below command

composer require spatie/laravel-sitemap

Step 2 : Publish config file

By default package will manage all the things, but if we need to change some config then we have to change in sitemap package config file and it will be generate by following command.

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

Step 3 : Create method to run package

Now create controller for run package method.

This package is so easy by use, we need to add one line to create our website sitemap.

<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Spatie\Sitemap\SitemapGenerator;

class SitemapController extends Controller {
   public function generate(Request $_request) {
       SitemapGenerator::create(url("/"))->writeToFile(public_path("sitemap.xml"));
   }
}

After run of above method you will find your generated sitemap following path : /public/sitemap.xml