PHPackages                             safaeean/laravel-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. [API Development](/categories/api)
4. /
5. safaeean/laravel-sitemap

ActiveLibrary[API Development](/categories/api)

safaeean/laravel-sitemap
========================

Create and generate sitemaps with ease

3.3(8y ago)21.3kMITPHPPHP ^7.0CI failing

Since Mar 8Pushed 2y ago1 watchersCompare

[ Source](https://github.com/safaeean/laravel-sitemap)[ Packagist](https://packagist.org/packages/safaeean/laravel-sitemap)[ Docs](https://github.com/safaeean/laravel-sitemap)[ RSS](/packages/safaeean-laravel-sitemap/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (8)Versions (7)Used By (0)

Generate sitemaps with ease
===========================

[](#generate-sitemaps-with-ease)

[![Latest Version on Packagist](https://camo.githubusercontent.com/3c307e53a45a105002d67da22374ee3771effbfea45bc5ebd8ebad9beb01ed39/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736166616565616e2f6c61726176656c2d736974656d61702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/safaeean/laravel-sitemap)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/6d22a00f60a1fb23424d5e84ee8d2adeeda66b44d0aa82f0b02567f0e1e33457/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f736166616565616e2f6c61726176656c2d736974656d61702f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/safaeean/laravel-sitemap)[![Quality Score](https://camo.githubusercontent.com/64836b69eaaad14765299dc3dd94aa22569df7e074f615f2bb6e302d9dbb9612/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f736166616565616e2f6c61726176656c2d736974656d61702e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/safaeean/laravel-sitemap)[![StyleCI](https://camo.githubusercontent.com/3f9a418861d9e78eb9bbbbd5483b34e80a792b055317f40d03a8e0e2d39c2650/68747470733a2f2f7374796c6563692e696f2f7265706f732f36353534393834382f736869656c64)](https://styleci.io/repos/65549848)[![Total Downloads](https://camo.githubusercontent.com/04f50140ca03a35f5a83cc5bd4dfa71faae8d7238e8140bc03128b117bf24330/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f736166616565616e2f6c61726176656c2d736974656d61702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/safaeean/laravel-sitemap)

This package can generate a sitemap without you having to add urls to it manually. This works by crawling your entire site.

```
use safaeean\Sitemap\SitemapGenerator;

SitemapGenerator::create('https://example.com')->writeToFile($path);
```

You can also create your sitemap manually:

```
use Carbon\Carbon;
use safaeean\Sitemap\Sitemap;
use safaeean\Sitemap\Tags\Url;

Sitemap::create()

    ->add(Url::create('/home')
        ->setLastModificationDate(Carbon::yesterday())
        ->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
        ->setPriority(0.1))

   ->add(...)

   ->writeToFile($path);
```

Or you can have the best of both worlds by generating a sitemap and then adding more links to it:

```
SitemapGenerator::create('https://example.com')
   ->getSitemap()
   ->add(Url::create('/extra-page')
        ->setLastModificationDate(Carbon::yesterday())
        ->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
        ->setPriority(0.1))

    ->add(...)

    ->writeToFile($path);
```

Postcardware
------------

[](#postcardware)

You're free to use this package (it's [MIT-licensed](LICENSE.md)), but if it makes it to your production environment we highly appreciated you sending us postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: safaeean, Samberstraat 69D, 2060 Antwerp, Belgium.

All postcards are published [on our website](https://mndco.ir/en/opensource/postcards).

Installation
------------

[](#installation)

First, install the package via composer:

```
composer require safaeean/laravel-sitemap
```

Next up: installing the service provider

```
// config/app.php
'providers' => [
    ...
    safaeean\Sitemap\SitemapServiceProvider::class,
];
```

If you want to update your sitemap automatically and frequently you need to perform [some extra steps](https://github.com/safaeean/laravel-sitemap#generating-the-sitemap-frequently).

Configuration
-------------

[](#configuration)

You can override the default options for the crawler. First publish the configuration:

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

This will copy the default config to `config/sitemap.php` where you can edit it.

```
use GuzzleHttp\RequestOptions;

return [

    /*
     * These options will be passed to GuzzleHttp\Client when it is created.
     * For in-depth information on all options see the Guzzle docs:
     *
     * http://docs.guzzlephp.org/en/stable/request-options.html
     */
    'guzzle_options' => [

        /*
         * Whether or not cookies are used in a request.
         */
        RequestOptions::COOKIES => true,

        /*
         * The number of seconds to wait while trying to connect to a server.
         * Use 0 to wait indefinitely.
         */
        RequestOptions::CONNECT_TIMEOUT => 10,

        /*
         * The timeout of the request in seconds. Use 0 to wait indefinitely.
         */
        RequestOptions::TIMEOUT => 10,

        /*
         * Describes the redirect behavior of a request.
         */
        RequestOptions::ALLOW_REDIRECTS => false,
    ]

];
```

Usage
-----

[](#usage)

### Generating a sitemap

[](#generating-a-sitemap)

The easiest way is to crawl the given domain and generate a sitemap with all found links. The destination of the sitemap should be specified by `$path`.

```
SitemapGenerator::create('https://example.com')->writeToFile($path);
```

The generated sitemap will look similiar to this:

```

        https://example.com
        2016-01-01T00:00:00+00:00
        daily
        0.8

        https://example.com/page
        2016-01-01T00:00:00+00:00
        daily
        0.8

    ...

```

### Customizing the sitemap generator

[](#customizing-the-sitemap-generator)

#### Changing properties

[](#changing-properties)

To change the `lastmod`, `changefreq` and `priority` of the contact page:

```
use Carbon\Carbon;
use safaeean\Sitemap\SitemapGenerator;
use safaeean\Sitemap\Tags\Url;

SitemapGenerator::create('https://example.com')
   ->hasCrawled(function (Url $url) {
       if ($url->segment(1) === 'contact') {
           $url->setPriority(0.9)
               ->setLastModificationDate(Carbon::create('2016', '1', '1'));
       }

       return $url;
   })
   ->writeToFile($sitemapPath);
```

#### Leaving out some links

[](#leaving-out-some-links)

If you don't want a crawled link to appear in the sitemap, just don't return it in the callable you pass to `hasCrawled `.

```
use safaeean\Sitemap\SitemapGenerator;
use safaeean\Sitemap\Tags\Url;

SitemapGenerator::create('https://example.com')
   ->hasCrawled(function (Url $url) {
       if ($url->segment(1) === 'contact') {
           return;
       }

       return $url;
   })
   ->writeToFile($sitemapPath);
```

#### Preventing the crawler from crawling some pages

[](#preventing-the-crawler-from-crawling-some-pages)

You can also instruct the underlying crawler to not crawl some pages by passing a `callable` to `shouldCrawl`.

```
use safaeean\Sitemap\SitemapGenerator;
use safaeean\Crawler\Url;

SitemapGenerator::create('https://example.com')
   ->shouldCrawl(function (Url $url) {
       // All pages will be crawled, except the contact page.
       // Links present on the contact page won't be added to the
       // sitemap unless they are present on a crawlable page.
       return $url->segment(1) !== 'contact';
   })
   ->writeToFile($sitemapPath);
```

#### Manually adding links

[](#manually-adding-links)

You can manually add links to a sitemap:

```
use safaeean\Sitemap\SitemapGenerator;
use safaeean\Sitemap\Tags\Url;

SitemapGenerator::create('https://example.com')
    ->getSitemap()
    // here we add one extra link, but you can add as many as you'd like
    ->add(Url::create('/extra-page')->setPriority(0.5))
    ->writeToFile($sitemapPath);
```

#### Adding alternates to links

[](#adding-alternates-to-links)

Multilingual sites may have several alternate versions of the same page (one per language). Based on the previous example adding an alterante can be done as follows:

```
use safaeean\Sitemap\SitemapGenerator;
use safaeean\Sitemap\Tags\Url;

SitemapGenerator::create('https://example.com')
    ->getSitemap()
    // here we add one extra link, but you can add as many as you'd like
    ->add(Url::create('/extra-page')->setPriority(0.5)->addAlternate('/extra-pagina', 'nl'))
    ->writeToFile($sitemapPath);
```

Note the `addAlternate` function which takes an alternate URL and the locale it belongs to.

### Manually creating a sitemap

[](#manually-creating-a-sitemap)

You can also create a sitemap fully manual:

```
use Carbon\Carbon;

Sitemap::create()
   ->add('/page1')
   ->add('/page2')
   ->add(Url::create('/page3')->setLastModificationDate(Carbon::create('2016', '1', '1')))
   ->writeToFile($sitemapPath);
```

### Creating a sitemap index

[](#creating-a-sitemap-index)

You can create a sitemap index:

```
use safaeean\Sitemap\SitemapIndex;

SitemapIndex::create()
    ->add('/pages_sitemap.xml')
    ->add('/posts_sitemap.xml')
    ->writeToFile($sitemapIndexPath);
```

You can pass a `safaeean\Sitemap\Tags\Sitemap` object to manually set the `lastModificationDate` property.

```
use safaeean\Sitemap\SitemapIndex;
use safaeean\Sitemap\Tags\Sitemap;

SitemapIndex::create()
    ->add('/pages_sitemap.xml')
    ->add(Sitemap::create('/posts_sitemap.xml')
        ->setLastModificationDate(Carbon::yesterday()))
    ->writeToFile($sitemapIndexPath);
```

the generated sitemap index will look similar to this:

```

      http://www.example.com/pages_sitemap.xml
      2016-01-01T00:00:00+00:00

      http://www.example.com/posts_sitemap.xml
      2015-12-31T00:00:00+00:00

```

Generating the sitemap frequently
---------------------------------

[](#generating-the-sitemap-frequently)

Your site will probably be updated from time to time. In order to let your sitemap reflect these changes you can run the generator periodically. The easiest way of doing this is do make use of Laravel's default scheduling capabilities.

You could setup an artisan command much like this one:

```
namespace App\Console\Commands;

use Illuminate\Console\Command;
use safaeean\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'))
            ->writeToFile(public_path('sitemap.xml'));
    }
}
```

That command should then be scheduled in the console kernel.

```
// app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
    ...
    $schedule->command('sitemap:generate')->daily();
    ...
}
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Testing
-------

[](#testing)

First start the test server in a seperate terminal session:

```
cd tests/server
./start_server.sh
```

With the server running you can execute the tests:

```
$ composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Freek Van der Herten](https://github.com/freekmurze)
- [All Contributors](../../contributors)

About safaeean
--------------

[](#about-safaeean)

safaeean is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://mndco.ir/opensource).

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity62

Established project with proven stability

 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

Every ~4 days

Total

6

Last Release

3010d ago

Major Versions

2.4.1 → 3.02018-03-09

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/19506782?v=4)[Hossein Piri](/maintainers/safaeean)[@safaeean](https://github.com/safaeean)

---

Top Contributors

[![safaeean](https://avatars.githubusercontent.com/u/19506782?v=4)](https://github.com/safaeean "safaeean (23 commits)")

---

Tags

laravellaravel-sitemapsafaeean

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[laravel/horizon

Dashboard and code-driven configuration for Laravel queues.

4.1k91.3M280](/packages/laravel-horizon)[spatie/laravel-responsecache

Speed up a Laravel application by caching the entire response

2.8k8.7M64](/packages/spatie-laravel-responsecache)[statamic/cms

The Statamic CMS Core Package

4.8k3.5M920](/packages/statamic-cms)[spatie/laravel-health

Monitor the health of a Laravel application

87411.3M153](/packages/spatie-laravel-health)[smodav/mpesa

M-Pesa API implementation

16167.1k1](/packages/smodav-mpesa)[sebastienheyd/boilerplate

Laravel Boilerplate based on AdminLTE 3 with blade components, user management, roles, permissions, logs viewer, ...

29419.5k3](/packages/sebastienheyd-boilerplate)

PHPackages © 2026

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