PHPackages                             leafs/sitemap - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. leafs/sitemap

ActiveLibrary[Framework](/categories/framework)

leafs/sitemap
=============

Sitemap generator for Leaf apps

v4.x-dev(1mo ago)03↑2900%MITPHPCI passing

Since Mar 29Pushed 1mo agoCompare

[ Source](https://github.com/leafsphp/sitemap)[ Packagist](https://packagist.org/packages/leafs/sitemap)[ Docs](https://leafphp.dev/)[ GitHub Sponsors](https://github.com/leafsphp)[ Fund](https://opencollective.com/leaf)[ RSS](/packages/leafs-sitemap/feed)WikiDiscussions v4.x Synced 1mo ago

READMEChangelogDependencies (3)Versions (1)Used By (0)

 [![](https://camo.githubusercontent.com/d98ee5e32c2ff016fdfdac6c42654a908f4cc34b229c7b00caacc5a717455ae8/68747470733a2f2f6c6561667068702e6465762f6c6f676f2d636972636c652e706e67)](https://camo.githubusercontent.com/d98ee5e32c2ff016fdfdac6c42654a908f4cc34b229c7b00caacc5a717455ae8/68747470733a2f2f6c6561667068702e6465762f6c6f676f2d636972636c652e706e67)

Sitemap Generator
=================

[](#sitemap-generator)

 **A simple and efficient sitemap generator for Leaf apps**

 [![Latest Stable Version](https://camo.githubusercontent.com/d31b8754de712f89e974d2f75b43970f42d7a2cd71f29353a9a62bf949982552/68747470733a2f2f706f7365722e707567782e6f72672f6c656166732f736974656d61702f762f737461626c65)](https://packagist.org/packages/leafs/sitemap) [![Total Downloads](https://camo.githubusercontent.com/b772a8148a096724d2dc3eaa18a743057db66b641b30e2374c2a53c97f879aa8/68747470733a2f2f706f7365722e707567782e6f72672f6c656166732f736974656d61702f646f776e6c6f616473)](https://packagist.org/packages/leafs/sitemap) [![License](https://camo.githubusercontent.com/e69e22ffb4d90f6005bc61722697f50cf459e5dcd32fb34ad56d68dd15da125f/68747470733a2f2f706f7365722e707567782e6f72672f6c656166732f736974656d61702f6c6963656e7365)](https://packagist.org/packages/leafs/sitemap)

Sitemap Generator
-----------------

[](#sitemap-generator-1)

**Sitemap Generator** is a simple and efficient tool for generating sitemaps in Leaf PHP. It helps you create XML sitemaps quickly and easily, ensuring your website is properly indexed by search engines. It generates a real sitemap.xml file in the public directory of your Leaf application, which can be accessed by search engines to crawl your website effectively, and has support for custom datasources, allowing you to generate sitemaps from various data sources such as databases, APIs, or static files.

🚀 Installation
--------------

[](#-installation)

**Using [Leaf CLI](https://cli.leafphp.dev) (Recommended)**:

```
leaf install sitemap
```

**Using [Composer](https://getcomposer.org/)**:

```
composer require leafs/sitemap
```

Quick Start
-----------

[](#quick-start)

Once installed, your sitemap will be automatically generated based on the routes defined in your Leaf application. You can customize the sitemap generation by adding a new datasource or by adding sitemap config options directly to your routes.

Regenerating Your Sitemap
-------------------------

[](#regenerating-your-sitemap)

Leaf automatically generates your sitemap when it doesn’t exist.

To regenerate your sitemap, you can:

- delete the existing sitemap.xml file
- or manually generate a new one:

    ```
    sitemap()->generate();
    ```

Auto Sitemaps
-------------

[](#auto-sitemaps)

Since sitemaps automatically use your Leaf routes, you can add some config options directly to your routes to customize how they appear in the sitemap. For example:

```
app()->get('/about', [
    'sitemap' => [
        'changefreq' => 'monthly',
        'priority' => 0.5,
    ],
    function() {
        echo 'About Us';
    }
]);
```

If you have a dynamic route like `/blog/{slug}`, you can also add the sitemap config to the route, you can tell the sitemap generator to replace the `{slug}` parameter with actual values from your model if you have one defined:

```
app()->get('/blog/{slug}', [
    'sitemap' => [
        'changefreq' => 'weekly',
        'priority' => 0.8,
        'model' => \App\Models\Post::class, // or 'posts' if you don't have a model but have a table named 'posts',
        'parameter' => 'slug', // the parameter in the route to replace with the model value,
        'exclude' => [
            'status' => 'draft' // you can also exclude certain items from the sitemap based on model attributes
        ]
    ],
    'BlogController@show'
]);
```

This is typically a faster way to generate sitemaps for dynamic routes, as you don't have to manually add a datasource and fetch the data yourself, the sitemap generator will handle it for you. So you will only need to manually add a datasource if you want to do more complex link generation like adding multiple URLs for the same route, or if you want to add URLs that are not defined as routes in your application.

Datasources
-----------

[](#datasources)

Your application may have dynamic routes, eg: `/blog/{slug}`. To include these routes in your sitemap, you can create a custom datasource that fetches the necessary data from your database and adds it to the sitemap. Here's an example of how to create a custom datasource for a blog:

```
sitemap()->source(function() {
    // Fetch blog posts from the database
    $posts = db()->select('posts')->get(); // or with a model like Post::all();

    // Add each post to the sitemap
    foreach ($posts as $post) {
        sitemap()->map('/blog/{slug}', "/blog/{$post->slug}", [
            'lastmod' => $post->updated_at,
            'changefreq' => 'weekly',
            'priority' => 0.8,
        ]);
    }
});

// original route
app()->get('/blog/{slug}', 'BlogController@show');
```

You can also add multiple URLs at once:

```
sitemap()->source(function() {
    // Fetch blog posts from the database
    $posts = db()->select('posts')->get(); // or with a model like Post::all();

    // Add each post to the sitemap
    foreach ($posts as $post) {
        sitemap()->map('/blog/{slug}', [
            "/en/blog/{$post->slug_en}" => [
                'lastmod' => $post->updated_at,
                'changefreq' => 'weekly',
                'priority' => 0.8,
            ],
            "/fr/blog/{$post->slug_fr}" => [
                'lastmod' => $post->updated_at,
                'changefreq' => 'weekly',
                'priority' => 0.8,
            ],
        ]);
    }
});

// original route
app()->get('/blog/{slug}', 'BlogController@show');
```

This will map the `/blog/{slug}` to the actual URLs of your blog posts, and include additional metadata such as last modification date, change frequency, and priority.

Add URL
-------

[](#add-url)

The examples above show how to map existing routes to actual URLs, but you can also add new URLs that are not defined as routes in your application:

```
sitemap()->source(function() {
    // Fetch blog posts from the database
    $posts = db()->select('posts')->get(); // or with a model like Post::all();

    // Add each post to the sitemap
    foreach ($posts as $post) {
        sitemap()->add("/blog/{$post->slug}", [
            'lastmod' => $post->updated_at,
            'changefreq' => 'weekly',
            'priority' => 0.8,
        ]);
    }
});
```

This will add the specified URLs to the sitemap without replacing any existing routes.

You can also use `add` without being dynamic:

```
sitemap()->source(function() {
    sitemap()->add('/about', [
        'changefreq' => 'monthly',
        'priority' => 0.5,
    ]);

    sitemap()->add('/contact', [
        'changefreq' => 'monthly',
        'priority' => 0.5,
    ]);
});
```

If you add a URL that already exists in the sitemap, it will be replaced with the new one. For example:

```
sitemap()->source(function() {
    ...

    sitemap()->add('/about', [
        'changefreq' => 'monthly',
        'priority' => 0.5,
    ]);
});

app()->get('/about', function() {
    echo 'About Us';
});
```

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance90

Actively maintained with recent releases

Popularity4

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity17

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

46d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/29547806?v=4)[Mychi](/maintainers/Mychi)[@mychi](https://github.com/mychi)

---

Top Contributors

[![mychidarko](https://avatars.githubusercontent.com/u/26604242?v=4)](https://github.com/mychidarko "mychidarko (8 commits)")

---

Tags

phpframeworkSitemapleaf

###  Code Quality

TestsPest

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/leafs-sitemap/health.svg)

```
[![Health](https://phpackages.com/badges/leafs-sitemap/health.svg)](https://phpackages.com/packages/leafs-sitemap)
```

###  Alternatives

[leafs/leaf

Elegant PHP for modern developers

1.3k44.3k9](/packages/leafs-leaf)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
