Developer Snippet Diary

Laravel xml sitemap generate dynamically for large no of records

We'll use the spatie/laravel-sitemap package to make this task easier. Here's a step-by-step guide:

1. Install the package using terminal or cmd

composer require spatie/laravel-sitemap

2.Create a Sitemap Controller:
Run the following command to generate a new SitemapController:

php artisan make:controller SitemapController

3.Modify the Sitemap Controller:
Open the newly created SitemapController.php file, which is located in the app/Http/Controllers directory. Add the necessary imports and modify the controller as follows:

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\Tags\Url;
use Spatie\Sitemap\SitemapIndex;
use Spatie\Sitemap\Tags\Sitemap as SitemapTag;
class SitemapController extends Controller
{
   public function index(){
        $sitemapIndex = SitemapIndex::create();
        $sitemapIndex->add(SitemapTag::create(url('sitemap/realestate.xml'))); //u can add more here
        return $sitemapIndex->toResponse(request());
    }
    public function realestate()
    {
        $realEstateCount = \App\Models\RealEstate::count();
        $sitemapFiles = ceil($realEstateCount / 3); //3 per page
        $sitemapIndex = SitemapIndex::create();
    
        for ($i = 1; $i <= $sitemapFiles; $i++) {
            $sitemapIndex->add(SitemapTag::create(url("sitemap/realestate/{$i}.xml")));
        } 
        return $sitemapIndex->toResponse(request());
    }
    public function realestateSitemap($page) //for singlepage
    {
        $limit = 3;
        $offset = ($page - 1) * $limit;
        $realEstates = \App\Models\RealEstate::offset($offset)->limit($limit)->get();
        $sitemap = Sitemap::create();
        foreach ($realEstates as $realEstate) {
            $url = url("realestate/details/{$realEstate->id}");
            $sitemap->add(Url::create($url));
        }
        return $sitemap->toResponse(request());
    }
}

4.Update the routes:
Modify the routes/web.php file to add routes for the sitemap index and each sitemap file:

Route::controller(\App\Http\Controllers\SitemapController::class)->group( function () {
    Route::get('/sitemap.xml', 'index');
    Route::get('/sitemap/realestate.xml', 'realestate');
    Route::get('/sitemap/realestate/{page}.xml', 'realestateSitemap');
});

OUTPUT:

http://127.0.0.1:8000/sitemap.xml

<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>http://127.0.0.1:8000/sitemap/realestate.xml</loc>
<lastmod>2023-04-04T06:08:13+00:00</lastmod>
</sitemap>
</sitemapindex>

http://127.0.0.1:8000/sitemap/realestate.xml

<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>http://127.0.0.1:8000/sitemap/realestate/1.xml</loc>
<lastmod>2023-04-04T05:42:30+00:00</lastmod>
</sitemap>
<sitemap>
<loc>http://127.0.0.1:8000/sitemap/realestate/2.xml</loc>
<lastmod>2023-04-04T05:42:30+00:00</lastmod>
</sitemap>
</sitemapindex>

http://127.0.0.1:8000/sitemap/realestate/2.xml

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<loc>http://127.0.0.1:8000/realestate/details/4</loc>
<lastmod>2023-04-04T05:44:05+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>http://127.0.0.1:8000/realestate/details/5</loc>
<lastmod>2023-04-04T05:44:05+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>http://127.0.0.1:8000/realestate/details/6</loc>
<lastmod>2023-04-04T05:44:05+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
</urlset>
Posted by: R GONDAL
Email: rizikmw@gmail.com