werkspot / sitemap-bundle
Bundle for generating dynamic sitemap.xml content with support for multiple sections
Installs: 166 236
Dependents: 1
Suggesters: 0
Security: 0
Stars: 10
Watchers: 37
Forks: 4
Open Issues: 4
Type:symfony-bundle
Requires
- php: ^7.4 || ^8.0
- symfony/framework-bundle: ^4.4 || ^5.2 || ^6.3
Requires (Dev)
- ext-simplexml: *
- friendsofphp/php-cs-fixer: ^v2.18
- mockery/mockery: ^1.4
- phpunit/phpunit: ^9.5
- symfony/asset: ^4.4 || ^5.2
- symfony/browser-kit: ^4.4 || ^5.2
- symfony/finder: ^4.4 || ^5.2
- symfony/templating: ^4.4 || 5.2
- symfony/twig-bridge: ^4.4 || ^5.2
- symfony/twig-bundle: ^4.4 || ^5.2
- symfony/yaml: ^4.4 || ^5.2
This package is auto-updated.
Last update: 2024-10-22 12:04:50 UTC
README
Bundle for generating dynamic sitemap.xml content with support for multiple sections and pages per section.
Install
# composer require werkspot/sitemap-bundle
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
Usage
The bundle generates a sitemap by looking for data providers.
A provider is a class that implements ProviderInterface and is tagged as werkspot.sitemap_provider
in the service container.
The bundle's service will gather data from all providers and create a sitemap section for everyone of them.
Each section can generate one or more pages.
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.
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
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.
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.
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.
Google Guidelines: https://support.google.com/webmasters/answer/2620865?hl=en
Note: The alternate links should include the Url itself, see the second note in the link above
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; }