SitemapXmlBundle для Symfony2

Этот бандл позволяет создать sitemap.xml для своего проекта на Symfony2.

GitHub

Установка через Composer - менеджер php пакетов

$ composer require evheniy/sitemap-xml-bundle "1.*"

Или добавить в composer.json:

"evheniy/sitemap-xml-bundle": "1.*"

AppKernel:

public function registerBundles()
    {
        $bundles = array(
            ...
            new Evheniy\SitemapXmlBundle\SitemapXmlBundle(),
            new AppBundle\AppBundle(),
            ...
        );
        ...

Самый простой способ установки - наследовать комнаду в своем бандле и реализовать защищенный метод setEntities(), в котором необходимо заполнить sitemap или sitemapIndex.

<?php
 
 namespace AppBundle\Command;
 
 use Evheniy\SitemapXmlBundle\Command\SiteMapDumpCommand as Command;
 
 class SiteMapDumpCommand extends Command
 {
     protected function setEntities()
     {
         $this->siteMapEntity = $this->serviceManager->createSiteMapEntity();
         $this->dumpEntity->setDomain('site.com');
         foreach ($pages as $page) {
             $this->siteMapEntity
                 ->addLocation(
                 $this->serviceManager->createLocationEntity()
                     ->setLocation($page['url'])
                     ->setLastmod(new \DateTime($page['date']))
             );
         }
     }
 }

Для больших sitemap.xml с количеством ссылок больше 50 000 есть возможность группировать файлы sitemap в sitemap index.

<?php
 
 namespace AppBundle\Command;
 
 use Evheniy\SitemapXmlBundle\Command\SiteMapDumpCommand as Command;
 
 class SiteMapDumpCommand extends Command
 {
     protected function setEntities()
     {
         $this->siteMapIndexEntity = $this->serviceManager->createSiteMapIndexEntity();
         $this->dumpEntity->setDomain('site.com');
         $siteMapEntity = $this->serviceManager->createSiteMapEntity();
         foreach ($pages as $page) {
             $siteMapEntity->addLocation(
                 $this->serviceManager->createLocationEntity()
                     ->setLocation($page['url'])
                     ->setLastmod(new \DateTime($page['date']))
             );
         }
         $this->siteMapIndexEntity->addSiteMap($siteMapEntity);
     }
 }

И последний шаг

app/console sitemap:dump

Документация

SitemapXmlBundle позволяет использовать fluent interface:

$this->siteMapIndexEntity
     ->addSiteMap(
         $this->serviceManager->createSiteMapEntity()
             ->addLocation(
                 $this->serviceManager->createLocationEntity()
                     ->setLocation('https://site.com/page1.html')
                     ->setLastmod(new \DateTime())
                 )
                 ->addLocation(
                     $this->serviceManager->createLocationEntity()
                         ->setLocation('https://site.com/page2.html')
                         ->setLastmod(new \DateTime())
                         ->addImage(
                             $this->serviceManager->createImageEntity()
                                 ->setLocation('https://site.com/logo.png')
                                 ->setTitle('Logo')
                     )
             )
     );

Больше документации:

Google помощь

Лицензия

Этот бандл использует лицензию MIT.

Демо

Теги: Sitemap.xml, Google, Symfony, Video, Image, News, Sitemap index


Похожие статьи

SimpleTaskFSMBundle для Symfony2

SimplePaginationBundle для Symfony2

MaterializeBundle для Symfony2

HTML5CacheBundle для Symfony2

HTML5VertiTemplateBundle для Symfony2

RobotsTxtBundle для Symfony2

GtmBundle (Google Tag Manager) для Symfony2

TwitterBootstrapBundle для Symfony2

JqueryBundle для Symfony2