werkspot/sitemap-bundle

支持多部分的动态生成 sitemap.xml 内容的包

安装次数: 165,450

依赖项: 1

建议者: 0

安全: 0

星级: 10

关注者: 37

分支: 4

开放问题: 4

类型:symfony-bundle

v4.1.0 2023-09-20 10:53 UTC

README

支持多部分和每部分的页面的动态生成 sitemap.xml 内容的包。

Travis build status Scrutinizer Code Quality

安装

# composer require werkspot/sitemap-bundle

更新你的路由

werkspot_sitemap:
    resource: "@WerkspotSitemapBundle/Resources/config/routing.yml"
    prefix:   /

这将使包监听 /sitemap.xml/sitemap.{section}.xml/sitemap.{section}.{page}.xml

使用方法

该包通过查找数据提供者来生成网站地图。

提供者是一个实现了 ProviderInterface 的类,并在服务容器中标记为 werkspot.sitemap_provider

该包的服务将从所有提供者中收集数据,并为每个提供者创建一个网站地图部分。

每个部分可以生成一个或多个页面。

单页面提供者

为了方便静态列表或不需要动态创建的页面创建网站地图,可以扩展 AbstractSinglePageSitemapProvider。这将只需提供部分名称和一个包含 URL 对象的 SitemapSectionPage。

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

这将生成包含 3 个页面的 /sitemap.default.xml 并在 /sitemap.xml 中添加链接。

多页面提供者

一个更复杂的提供者可以扩展 AbstractSitemapProvider。在这种情况下,提供者可以选择生成多少页面以及每页返回哪些结果。

示例

以下将生成 /sitemap.products.xml,如果数量太多,它将分成多个页面:/sitemap.products.1.xml/sitemap.products.2.xml 等,并在 /sitemap.xml 中添加所有正确的链接,以便进行索引。

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

添加替代语言页面

要向网站地图添加页面的翻译链接,可以在 URL 中使用 AlternateLink 并添加这些链接。

谷歌指南: https://support.google.com/webmasters/answer/2620865?hl=en

注意:替代链接应包括 URL 本身,请参阅上面链接的第二点说明。

示例

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