2017 © Pedro Peláez
 

symfony-bundle sitemap-bundle

Bundle for generating dynamic sitemap.xml content with support for multiple sections

image

werkspot/sitemap-bundle

Bundle for generating dynamic sitemap.xml content with support for multiple sections

  • Monday, June 25, 2018
  • by jeroenvdheuvel
  • Repository
  • 39 Watchers
  • 5 Stars
  • 58,654 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 2 Open issues
  • 8 Versions
  • 12 % Grown

The README.md

Werkspot sitemap bundle

Bundle for generating dynamic sitemap.xml content with support for multiple sections and pages per section., (*1)

Travis build status Scrutinizer Code Quality, (*2)

Install

# composer require werkspot/sitemap-bundle, (*3)

Update your routing

werkspot_sitemap:
    resource: "@WerkspotSitemapBundle/Resources/config/routing.yml"
    prefix:   /

That will make the bundle listen to /sitemap.xml, /sitemap.{section}.xml and /sitemap.{section}.{page}.xml, (*4)

Usage

The bundle generates a sitemap by looking for data providers., (*5)

A provider is a class that implements ProviderInterface and is tagged as werkspot.sitemap_provider in the service container., (*6)

The bundle's service will gather data from all providers and create a sitemap section for everyone of them., (*7)

Each section can generate one or more pages., (*8)

Single page provider

To facilitate sitemap creation for static lists things or pages that do not need to be created dynamically AbstractSinglePageSitemapProvider can be extended. That will only require providing a section name and a SitemapSectionPage with Url objects., (*9)

Example
namespace AppBundle\Sitemap;

use Werkspot\Bundle\SitemapBundle\Provider\AbstractSinglePageSitemapProvider;
use Werkspot\Bundle\SitemapBundle\Sitemap\SitemapSectionPage;
use Werkspot\Bundle\SitemapBundle\Sitemap\Url;

class StaticPageSitemapProvider extends AbstractSinglePageSitemapProvider
{
    /**
     * @return string
     */
    public function getSectionName()
    {
        return 'default';
    }

    /**
     * @return SitemapSectionPage
     */
    public function getSinglePage()
    {
        $page = new SitemapSectionPage();

        $urlRoute = $this->generateUrl('home');
        $page->addUrl(new Url($urlRoute, Url::CHANGEFREQ_WEEKLY, 1.0));

        $urlRoute = $this->generateUrl('some_page');
        $page->addUrl(new Url($urlRoute, Url::CHANGEFREQ_WEEKLY, 0.6));

        $urlRoute = $this->generateUrl('another_page');
        $page->addUrl(new Url($urlRoute, Url::CHANGEFREQ_WEEKLY, 0.6));

        return $page;
    }
}

This will generate /sitemap.default.xml with 3 pages and put a link to it in /sitemap.xml, (*10)

Multiple page provider

A more complex provider can extend AbstractSitemapProvider. In that case the provider can select how many pages it wants to generate and what results to return in every page., (*11)

Example

The following will generate /sitemap.products.xml and if the count is too high it will split in multiple pages: /sitemap.products.1.xml, /sitemap.products.2.xml etc and put all the right links to these in /sitemap.xml for indexing to happen., (*12)

namespace AppBundle\Sitemap;

use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Werkspot\Bundle\SitemapBundle\Provider\AbstractSitemapProvider;
use Werkspot\Bundle\SitemapBundle\Sitemap\SitemapSectionPage;
use Werkspot\Bundle\SitemapBundle\Sitemap\Url;
use AppBundle\Domain\Entity\Repository\ProductRepository;
use AppBundle\Domain\Entity\Product;

class ProductSitemapProvider extends AbstractSitemapProvider
{
    /**
     * @var ProductRepository
     */
    private $productRepository;

    /**
     * @param UrlGeneratorInterface $urlGenerator
     * @param ProductRepository $productRepository
     */
    public function __construct(UrlGeneratorInterface $urlGenerator, ProductRepository $productRepository)
    {
        parent::__construct($urlGenerator);
        $this->productRepository = $productRepository;
    }

    /**
     * @param int $pageNumber
     * @return SitemapSectionPage
     */
    public function getPage($pageNumber)
    {
        $products = $this->productRepository->getProductsForSitemapPage(
            $pageNumber,
            $this->getMaxItemsPerPage()
        );

        $page = new SitemapSectionPage();
        foreach ($products as $product) {
            $urlRoute = $this->generateUrl('product_details', [
                'slug' => $product->getSlug()
            ]);
            $page->addUrl(new Url($urlRoute, Url::CHANGEFREQ_MONTHLY, 0.6));
        }
        return $page;
    }

    /**
     * @return string
     */
    public function getSectionName()
    {
        return 'products';
    }

    /**
     * @return int
     */
    public function getCount()
    {
        return $this->productRepository->getTotalCount();
    }
}

Adding alternate language pages

To add links to translations for pages to the sitemap, you can use AlternateLink and add these to a Url., (*13)

Google Guidelines: https://support.google.com/webmasters/answer/2620865?hl=en, (*14)

Note: The alternate links should include the Url itself, see the second note in the link above, (*15)

Example

    /**
     * @return SitemapSectionPage
     */
    public function getSinglePage()
    {
        $page = new SitemapSectionPage();

        $urlRoute = $this->generateUrl('home');
        $urlRouteDe = $this->generateUrl('home', ['_locale' => 'de']);
        $urlRouteFr = $this->generateUrl('home', ['_locale' => 'fr']);

        $sitemapUrl = new Url($urlRoute, Url::CHANGEFREQ_WEEKLY, 1.0);
        $sitemapUrl->addAlternateLink(new AlternateLink($urlRouteDe, 'de'));
        $sitemapUrl->addAlternateLink(new AlternateLink($urlRouteFr, 'fr'));
        $sitemapUrl->addAlternateLink(new AlternateLink($urlRoute, 'x-default')); // Country select page
        // Or
        $sitemapUrl->addAlternateLink(new AlternateLink($urlRoute, 'en')); 

        $page->addUrl($sitemapUrl);

        return $page;
    }

The Versions

02/03 2018

dev-symfony4

dev-symfony4

Bundle for generating dynamic sitemap.xml content with support for multiple sections

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thanos Polymeneas

bundle symfony seo sitemap

09/08 2017

1.2.0

1.2.0.0

Bundle for generating dynamic sitemap.xml content with support for multiple sections

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thanos Polymeneas

bundle symfony seo sitemap

03/11 2016

v1.1.2

1.1.2.0

Bundle for generating dynamic sitemap.xml content with support for multiple sections

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thanos Polymeneas

bundle symfony seo sitemap

09/05 2016

v1.1.1

1.1.1.0

Bundle for generating dynamic sitemap.xml content with support for multiple sections

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thanos Polymeneas

bundle symfony seo sitemap

22/01 2016

v1.1.0

1.1.0.0

Bundle for generating dynamic sitemap.xml content with support for multiple sections

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thanos Polymeneas

bundle symfony seo sitemap

31/07 2015

v1.0.0

1.0.0.0

Bundle for generating dynamic sitemap.xml content with support for multiple sections

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thanos Polymeneas

bundle symfony seo sitemap