m-adamski/symfony-sitemap-bundle

Symfony 的 Sitemap 包

安装: 336

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 3

分支: 0

开放问题: 0

类型:symfony-bundle

1.0.1 2021-06-18 13:00 UTC

This package is auto-updated.

Last update: 2024-09-18 20:04:35 UTC


README

此包创建动态的 XML 格式 sitemap。

安装

此包可以通过 Composer 安装

composer require m-adamski/symfony-sitemap-bundle

如何使用它?

此包提供 Sitemap 控制器和一个配置好的路由文件,您只需将其导入本地配置。您可以通过在 config/routes.yaml 文件中添加 sitemap 部分来完成此操作

sitemap:
    resource: '@SitemapBundle/Resources/config/routes.yaml'

下一步是标记要添加到 sitemap 文件的路由。此包支持多种配置

index:
    path: /
    methods: [ GET ]
    controller: App\Controller\DefaultController::index
    defaults:
        _sitemap: 1.00
index:
    path: /
    methods: [ GET ]
    controller: App\Controller\DefaultController::index
    defaults:
        _sitemap: true
index:
    path: /
    methods: [ GET ]
    controller: App\Controller\DefaultController::index
    defaults:
        _sitemap:
            priority: 1.00
            change_frequency: 'monthly'
            last_modification: '2021-01-01 12:00:00'

关于动态生成的路由?有时我们希望包含在 sitemap 中的 URL 是使用一个或多个参数动态生成的。在这种情况下,可以使用 generator 参数

city:
    path: /city/{cityName}
    methods: [ GET ]
    controller: App\Controller\DefaultController::city
    defaults:
        _sitemap:
            generator: App\Model\SitemapGenerator::generateCity
<?php

namespace App\Model;

use Adamski\Symfony\SitemapBundle\Model\SitemapGeneratorInterface;

class SitemapGenerator implements SitemapGeneratorInterface {

    public function generateCity(): array {
        return [
            ["cityName" => "New York"],
            ["cityName" => "Oslo"],
            ["cityName" => "Warsaw"]
        ];
    }
}

重要:SitemapGenerator 对象必须在 DI 中标记为公开。

App\Model\SitemapGenerator:
    public: true

结果,应该在 sitemap 文件中生成三个额外的条目

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>https:///city/New%20York</loc>
    </url>
    <url>
        <loc>https:///city/Oslo</loc>
    </url>
    <url>
        <loc>https:///city/Warsaw</loc>
    </url>
</urlset>

此包还支持国际化路由

home:
    path:
        pl: /pl
        en: /en
    methods: [ GET ]
    controller: App\Controller\DefaultController::staticPage
    defaults:
        _sitemap: true

生成的 Sitemap XML

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:xhtml="http://www.w3.org/1999/xhtml">
    <url>
        <loc>https:///pl</loc>
        <xhtml:link rel="alternate" hreflang="pl" href="https:///pl"/>
        <xhtml:link rel="alternate" hreflang="en" href="https:///en"/>
    </url>
    <url>
        <loc>https:///en</loc>
        <xhtml:link rel="alternate" hreflang="en" href="https:///en"/>
        <xhtml:link rel="alternate" hreflang="pl" href="https:///pl"/>
    </url>
</urlset>

许可证

MIT