PHPackages                             jantaodev/sitemap-bundle - 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. jantaodev/sitemap-bundle

ActiveSymfony-bundle

jantaodev/sitemap-bundle
========================

Symfony bundle that provides sitemap XML generation

0.2(4y ago)0149MITPHPPHP &gt;=7.2.5

Since Sep 4Pushed 4y agoCompare

[ Source](https://github.com/JantaoDev/SitemapBundle)[ Packagist](https://packagist.org/packages/jantaodev/sitemap-bundle)[ RSS](/packages/jantaodev-sitemap-bundle/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (2)DependenciesVersions (3)Used By (0)

JantaoDevSitemapBundle
======================

[](#jantaodevsitemapbundle)

This Symfony bundle provides sitemap XML generation.

Features:

- advanced robots.txt configuration
- several hosts support
- optional sitemap GZip compression
- sitemap constraints (10 MBytes/50 000 items per file) support
- route parameters iteration

Requirements:

- PHP 7.2
- Symfony 5
- Doctrine 2

1. Installation
---------------

[](#1-installation)

Run `composer require jantaodev/sitemap-bundle`.

Register the bundle in the `app/AppKernel.php` file (optional):

```
...
public function registerBundles()
{
    $bundles = array(
        ...
        new JantaoDev\SitemapBundle\JantaoDevSitemapBundle(),
        ...
    );
...
```

Add host name in `app/config/parameters.yml`

```
parameters:
    ...
    router.request_context.host:   example.com
    router.request_context.scheme: http
```

or in `app/config/config.yml`

```
...
jantao_dev_sitemap:
    hosts:
        - example.com
```

Now you can use the bundle.

2. Basic usage
--------------

[](#2-basic-usage)

There are several ways to add route to sitemap.

### 2.1. Annotation, yaml, XML (for static routes without parameters)

[](#21-annotation-yaml-xml-for-static-routes-without-parameters)

Annotation simple example:

```
/**
 * @Route("/", name="homepage", options={"sitemap" = true})
 */
```

Yaml simple example:

```
homepage:
    path: /
    defaults: { _controller: "AppBundle:Default:index" }
    options:
        sitemap: true
```

XML simple example:

```

    AppBundle:Default:index
    true

```

Annotation full example:

```
/**
 * @Route("/", name="homepage", options={"priority" = 0.5, "changeFreq" = "monthly", "lastMod" = "2017-02-23T13:14:15+02:00" })
 */
```

Yaml full example:

```
homepage:
    path: /
    defaults: { _controller: "AppBundle:Default:index" }
    options:
        sitemap:
            priority: 0.5
            changeFreq: monthly
            lastMod: "2017-02-23T13:14:15+02:00"
```

XML full example:

```

    AppBundle:Default:index

        {"priority":"0.5", "changeFreq":"monthly", "lastMod":"2017-02-23T13:14:15+02:00"}

```

There are three parameters:

- priority (real number from 0 to 1)
- changeFreq (one of values: always, never, hourly, daily, weekly, monthly, yearly)
- lastMod (see )

It`s not necessary to specify all parameters.

### 2.2. Bundle configuration in app/config/config.yml

[](#22-bundle-configuration-in-appconfigconfigyml)

Simple example for static route:

```
jantao_dev_sitemap:
    ...
    sitemap:
        homepage: ~
```

Full example for static route:

```
jantao_dev_sitemap:
    ...
    sitemap:
        homepage:
            priority: 0.5
            change_freq: monthly
            last_mod: "2017-02-23T13:14:15+02:00"
```

Full example for dynamic route with parameters:

```
jantao_dev_sitemap:
    ...
    sitemap:
        homepage:
            priority: 0.5
            change_freq: monthly
            last_mod: "2017-02-23T13:14:15+02:00"
            route_parameters:
                slug: "someslug"
```

Iterate data for route parameters with array iterator:

```
jantao_dev_sitemap:
    ...
    sitemap:
        homepage:
            iterator: array
            values:
                - slugone
                - slugtwo
                - slugthree
            priority: 0.5
            change_freq: monthly
            last_mod: "2017-02-23T13:14:15+02:00"
            route_parameters:
                slug: "->"
```

Iterate data for route parameters with array iterator (example 2):

```
jantao_dev_sitemap:
    ...
    sitemap:
        homepage:
            iterator: array
            values:
                - {slug: slugone, change: monthly}
                - {slug: slugtwo, change: hourly}
                - {slug: slugthree, change: dayly}
            priority: 0.5
            change_freq: "->change"
            last_mod: "2017-02-23T13:14:15+02:00"
            route_parameters:
                slug: "->slug"
```

Iterate data for route parameters with Doctrine iterator:

```
jantao_dev_sitemap:
    ...
    sitemap:
        homepage:
            iterator: doctrine
            query: 'SELECT p FROM AppBundle:Page p WHERE p.enabled = true'
            priority: 0.5
            change_freq: monthly
            last_mod: "->modifiedAt"
            route_parameters:
                slug: "->slug"
                categorySlug: "->category->slug"
```

3. Execute sitemap generation
-----------------------------

[](#3-execute-sitemap-generation)

There are two ways to execute sitemap generation:

1. via console

```
bin/console jantao_dev:sitemap:generate

```

2. via service

```
$this->get('jantao_dev.sitemap')->generate();
```

4. Advanced configuration
-------------------------

[](#4-advanced-configuration)

Full bundle configuration:

```
jantao_dev_sitemap:
    hosts:
        - example.com
    gzip: false
    scheme: https
    robots:
        # Robots.txt configuration
    sitemap:
        # Sitemap configuration
```

Parameters description:

ParameterDescriptionhostsFor one host configuration see chapter 1, For several hosts see chapter 4.2gzipEnable sitemap GZip compressionschemeHost scheme mode (http, https, https\_only\*)robotsSee chapter 4.1sitemapSee chapters 2.2 and 4.2- https\_only means that site is available only over https

### 4.1. Robots.txt configuration

[](#41-robotstxt-configuration)

Full robots.txt configuration:

```
jantao_dev_sitemap:
    ...
    robots:
        allow:
            "/ajax/": ~
            "/system/": "Googlebot"
        disallow:
            "/admin/": ~
            "/otherpath": "Googlebot"
        crawl_delay: 5
        clean_param:
            "/photo": ["query=1", "page=2"]
```

Parameters description:

ParameterDescriptionallowAllow entries array, key is a path, value is a user-agent (null means "\*")disallowDisallow entries arraycrawl\_delayCrawl delay parameterclean\_paramClean param entries array, key is a path, value is an array of parametersFor more details about robots.txt parameters see [this](https://yandex.ru/support/webmaster/controlling-robot/robots-txt.xml) or [this](https://developers.google.com/webmasters/control-crawl-index/docs/robots_txt).

### 4.2. Several hosts support

[](#42-several-hosts-support)

Several hosts support allows you to generate different sitemaps to several hosts.

So, enable several hosts support, list your hosts in bundle configuration:

```
jantao_dev_sitemap:
    ...
    hosts:
        example.com
        foo.com
        bar.com
```

Enable robots.txt switching controller in router (add following lines to `app/config/routing.yml`):

```
jantao_dev_sitemap:
    resource: "@JantaoDevSitemapBundle/Resources/config/routing.xml"
    prefix:   /
```

By default all ruotes will be added to each host sitemaps.

But in sitemap section in bundle configuration you can specify host.

Example:

```
jantao_dev_sitemap:
    ...
    hosts:
        example.com
        foo.com
        bar.com
    ...
    sitemap:
        homepage: ~
        about@foo.com: ~
```

In this example homepage will be added to all sitemaps, but about page will be add only to "foo.com" sitemap.

Example 2 (different slugs):

```
jantao_dev_sitemap:
    ...
    hosts:
        example.com
        ru.example.com
        de.example.com
    ...
    sitemap:
        homepage@example.com:
            iterator: doctrine
            query: 'SELECT p FROM AppBundle:Page p WHERE p.enabled = true'
            priority: 0.5
            change_freq: monthly
            last_mod: "->modifiedAt"
            route_parameters:
                slug: "->slugEn"
        homepage@ru.example.com:
            iterator: doctrine
            query: 'SELECT p FROM AppBundle:Page p WHERE p.enabled = true'
            priority: 0.5
            change_freq: monthly
            last_mod: "->modifiedAt"
            route_parameters:
                slug: "->slugRu"
        homepage@de.example.com:
            iterator: doctrine
            query: 'SELECT p FROM AppBundle:Page p WHERE p.enabled = true'
            priority: 0.5
            change_freq: monthly
            last_mod: "->modifiedAt"
            route_parameters:
                slug: "->slugDe"
```

5. Add custom event listener
----------------------------

[](#5-add-custom-event-listener)

To add a URL using your own logic, you can use event listener or event subscriber.

Example `AppBundle/EventListener/SomeListener.php`:

```
