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

MahmoudAdelAli's avatar

Generate Site-Map

Hi, I want to create a sitemap for a Medical Blog. I came across "spatie/laravel-sitemap" and attempted to utilize it. However, I encountered difficulty in adding each blog to the sitemap as the documentation suggests rewriting the entire sitemap every time a blog is added. Does anyone have a solution for this package?

0 likes
10 replies
Braunson's avatar
Braunson
Best Answer
Level 18

Have you seen this Laravel News article? They have an example on generating a sitemap for say posts (assuming you've defined the appropriate URL schema in the model)

use Spatie\Sitemap\Sitemap;
 
Sitemap::create()
    ->add(Post::all()
    ->writeToFile(public_path('sitemap.xml'));

This is the manual way and if you want more granular control of your sitemap.

Alternatively there is an automatic crawling method:

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

Regardless you'll want to have an event (on create/update of a blog post) that fires a job or does the above logic synchronous to generate an updated sitemap whenever you add a new post.

There's a number of ways you can automatically generate the sitemap but in the end, you'll need to cache/save it after generation and then re-generate it when things change (i.e. new post).

3 likes
MahmoudAdelAli's avatar

@Braunson Yes i see it , but i just make-sure is am at the right way , the second way is rewrite at whole file ?

Braunson's avatar

@MahmoudAdelAli Yep, either way they re-write to a file.

The alternative if you wanted something dynamic is to setup a route that just runs the Spatie Sitemap generator and then output the results though generally you don't want to run live as it may take resources/time.

Instead you can either write to file or cache, but regardless they need to be updated/re-written/re-cached whenever you have a blog post update (which is where event/listeners come in)

gych's avatar

Its also possible to have a sitemap without the use of this package.

This is a simple example that should get you in the right direction: https://laraget.com/blog/generate-a-simple-xml-sitemap-using-laravel

When using this approach you can also add all routes dynamically instead of adding them manually by using app()->router->getRoutes();. But then you'll have to exclude the routes you don't want to be used in the sitemap. This is not a must but it depends on how much routes you have to add manually.

To those routes you can then add all the routes of the blogs by using the base blog route and adding the unique slug or id to that.

In my project I send all the url's from the backend to sitemap.blade as $URLs without having to add anything manually.

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    @foreach($URLs as $URL)
    <url>
        <loc>{{ $URL }}</loc>
    </url>
    @endforeach
</urlset>
1 like
MahmoudAdelAli's avatar

@gych This sounds great , but the issue here not the static routes like " About / contact / etc .. " i talk about the dynamic one like blogs , services and doctors pages that the client are create , so you think this good way to use within ?

JussiMannisto's avatar

@MahmoudAdelAli Just a side note: if you have a lot of dynamic pages (50k+), you can't use a single sitemap file. In that case you have to create multiple sitemap files and reference them from a sitemap index document.

2 likes
MahmoudAdelAli's avatar

@JussiMannisto Yes understand what you mean , like

->add($this->build_index(Article::active()->get(), 'sitemap_articles.xml'))
 ->add($this->build_index(Partner::active()->get(), 'sitemap_partners.xml'))

i just try to understand at the first how does the package works

gych's avatar

@MahmoudAdelAli Yes its possible to add dynamic url's from blog posts. You can completely customize it to your needs. Even multiple sitemaps is possible if there's ever need for it.

MahmoudAdelAli's avatar

Just note : I'm using filament php , so it's difficult to use the Create method each time i create blog

#Edit Page , And same for Create Page 
    protected function handleRecordUpdate(Model $record, array $data): Model
    {
        $path = public_path('sitemap.xml');
        $site_url = url('/');
        SitemapGenerator::create($site_url)->writeToFile($path);
        return parent::handleRecordUpdate($record, $data); // TODO: Change the autogenerated stub
    }

Please or to participate in this conversation.