evheniy / sitemap-xml-bundle
The SitemapXmlBundle adds the ability to generate sitemap.xml file to your application.
Installs: 18 503
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=5.3.0
- symfony/symfony: >=2.3
Requires (Dev)
- satooshi/php-coveralls: dev-master
This package is not auto-updated.
Last update: 2024-11-09 18:59:22 UTC
README
This bundle provides sitemap.xml generator for Symfony2
Installation
$ composer require evheniy/sitemap-xml-bundle "1.*"
Or add to composer.json
"evheniy/sitemap-xml-bundle": "1.*"
AppKernel:
public function registerBundles()
{
$bundles = array(
...
new Evheniy\SitemapXmlBundle\SitemapXmlBundle(),
new AppBundle\AppBundle(),
...
);
...
...
You should set bundle before main bundle (in our example it's AppBundle) and extend SiteMapDumpCommand ( setEntities() method )
The easy way to create sitemap:
<?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']))
);
}
}
}
If there are more than 50 000 links you should use 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);
}
}
The last step
app/console sitemap:dump
Documentation
SitemapXmlBundle made by using fluent interface:
$this->siteMapIndexEntity
->addSiteMap(
$this->serviceManager->createSiteMapEntity()
->addLocation(
$this->serviceManager->createLocationEntity()
->setLocation('http://site.com/page1.html')
->setLastmod(new \DateTime())
)
->addLocation(
$this->serviceManager->createLocationEntity()
->setLocation('http://site.com/page2.html')
->setLastmod(new \DateTime())
->addImage(
$this->serviceManager->createImageEntity()
->setLocation('http://site.com/logo.png')
->setTitle('Logo')
)
)
);
More details:
- Service manager
- Dump manager
- Dump entity
- Site map index entity
- Site map entity
- Location entity
- Image entity
- Video entity
- News entity
License
This bundle is under the MIT license.