PHP code example of werkspot / sitemap-bundle

1. Go to this page and download the library: Download werkspot/sitemap-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

werkspot / sitemap-bundle example snippets


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;
    }
}

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();
    }
}

    /**
     * @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;
    }