Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sulu sitemap provider for static Symfony routes

Sulu Sitemap mit Symfony Routes erweitern

Mit der Klasse SitemapProvider.php könnt ihr eure Sulu Sitemap erweitern. In der Klasse müssen lediglich die Routen in dem Array $routeNames angegeben werden. Hier verwendet ihr die Namen, wie ihr sie in eurer routes.yaml definiert habt. Diese werden dann in die allgemeine Sitemap aufgenommen.

Eine detaillierte Beschreibung findet ihr in meinem Blog Post https://blog.stefaniedrost.com/sulu-sitemap-mit-symfony-routen-erweitern/

<?php
namespace App\Service;
use Sulu\Bundle\WebsiteBundle\Sitemap\Sitemap;
use Sulu\Bundle\WebsiteBundle\Sitemap\SitemapProviderInterface;
use Sulu\Bundle\WebsiteBundle\Sitemap\SitemapUrl;
use Symfony\Component\Routing\RouterInterface;
class SitemapProvider implements SitemapProviderInterface
{
private $router;
// put your symfony route names here
protected $routeNames = [
'impressum',
'dataprotection',
'contact',
];
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
public function build($page, $scheme, $host)
{
$result = [];
$changeDate = new \DateTime();
foreach ($this->routeNames as $routeName) {
$result[] = new SitemapUrl(
$scheme . '://' . $host . $this->router->getRouteCollection()->get($routeName)->getPath(),
'de',
'de',
$changeDate
);
}
return $result;
}
public function getAlias()
{
return 'mySitemap';
}
public function createSitemap($scheme, $host)
{
return new Sitemap($this->getAlias(), $this->getMaxPage($scheme, $host));
}
public function getMaxPage($scheme, $host)
{
if ($host !== $this->router->getContext()->getHost()) {
// If the pages are only for a specific
return 0;
}
return ceil(count($this->routeNames) / self::PAGE_SIZE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.