Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zunkt's avatar
Level 2

Sitemap Generate all but i just want route

Hi everyone! I'm just beginner in laravel. Now i want to create sitemap that show only route and add to sitemap.xml

Package i use: https://github.com/spatie/laravel-sitemap

My Commands:


namespace App\Console\Commands;

use Illuminate\Console\Command;
use Psr\Http\Message\UriInterface;
use Spatie\Crawler\Crawler;
use Spatie\Sitemap\SitemapGenerator;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Generate the sitemap.';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // modify this to your own needs
        SitemapGenerator::create(config('app.url'))
            ->configureCrawler(function (Crawler $crawler) {
                $crawler->ignoreRobots();
            })
            ->shouldCrawl(function (UriInterface $url) {
                return strpos($url->getPath(), '/home')||strpos($url->getPath(), '/setting');
            })
            ->writeToFile(public_path('sitemap.xml'));
    }
}

In my env:

APP_URL=http://localhost
In my web:
Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');
Route::resource('setting', 'SettingController');

My result:

 <loc>http://localhost/e-commerce-store/vendor/symfony/string/Slugger/Asc
iiSlugger.php</loc>
        <lastmod>2020-07-20T10:06:55+00:00</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>
    <url>
        <loc>http://localhost/e-commerce-store/vendor/symfony/string/Slugger/Slu
ggerInterface.php</loc>
        <lastmod>2020-07-20T10:06:55+00:00</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>
    <url>
        <loc>http://localhost/e-commerce-store/vendor/symfony/string/UnicodeStri
ng.php</loc>
        <lastmod>2020-07-20T10:06:40+00:00</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>
    <url>
        <loc>http://localhost/e-commerce-store/vendor/symfony/string/composer.js
on</loc>
        <lastmod>2020-07-20T10:06:40+00:00</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>
    <url>
        <loc>http://localhost/e-commerce-store/vendor/symfony/translation-contra
cts/</loc>
        <lastmod>2020-07-20T10:06:29+00:00</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>
0 likes
2 replies
JohnBraun's avatar

You can use ->add() to specify which routes should be added.

public function handle()
{
    // modify this to your own needs
    SitemapGenerator::create(config('app.url'))
        ->configureCrawler(function (Crawler $crawler) {
            $crawler->ignoreRobots();
        })
        ->add(Url::create('/home')
            ->setLastModificationDate(Carbon::yesterday())
            ->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
            ->setPriority(0.1))
        ->writeToFile(public_path('sitemap.xml'));
}
zunkt's avatar
Level 2

@johnbraun

Thanks for reply my thread. It's seem like manual to create Url. I just test it. But it's just create simple like:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
    <url>
        <loc>http://localhost/e-commerce/routes/web</loc>
        <lastmod>2020-07-19T00:00:00+00:00</lastmod>
        <changefreq>yearly</changefreq>
        <priority>0.1</priority>
    </url>
</urlset>

I want this can generate only route of web. I just see a package about config CustomCrawlProfile and i'm doing like this:

<?php

namespace Spatie\Sitemap\Crawler;

use Spatie\Crawler\CrawlProfile;
use Psr\Http\Message\UriInterface;

class CustomCrawlProfile extends CrawlProfile
{
    public function shouldCrawl(UriInterface $url): bool
    {
        if ($url->getHost() !== 'localhost') {
            return false;
        }

        return $url->getPath() === '/';
    }
}

In GenerateSitemap:

public function handle()
    {
        // modify this to your own needs
        SitemapGenerator::create(config('app.url'))
            ->shouldCrawl(function (UriInterface $url) {
                return strpos($url->getPath(), '/web');
            })
            ->writeToFile(public_path('sitemap.xml'));
    }

In .env:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:9LMUlL/+/PnLdjbnW4SHEMpEsfN0chH7HIyE30HR2PQ=
APP_DEBUG=true
APP_URL=http://localhost/e-commerce/routes/
LOG_CHANNEL=stack

And it's just show about:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
</urlset>

Please or to participate in this conversation.