Этот бандл позволяет создать sitemap.xml для своего проекта на Symfony2.
Установка через 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') ) ) );
Больше документации:
- Service manager
- Dump manager
- Dump entity
- Site map index entity
- Site map entity
- Location entity
- Image entity
- Video entity
- News entity
Лицензия
Этот бандл использует лицензию MIT.