dpn/xml-sitemap-bundle

通过从网站的路线中提取网站地图信息,为您的首选搜索引擎生成XML网站地图。

安装次数: 32,948

依赖关系: 1

建议者: 2

安全性: 0

星标: 27

关注者: 3

分支: 8

开放问题: 0

类型:symfony-bundle

v2.1.0 2017-02-06 15:05 UTC

This package is auto-updated.

Last update: 2024-09-21 20:50:46 UTC


README

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version License

此捆绑包通过从应用程序的路线中提取网站地图信息为您的首选搜索引擎生成XML网站地图。此外,您还可以创建自己的生成器以提供URL。生成的网站地图遵循网站地图协议

安装

  1. 使用composer安装

     composer require dpn/xml-sitemap-bundle
    
  2. 在内核中启用捆绑包

    // app/AppKernel.php
    
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Dpn\XmlSitemapBundle\DpnXmlSitemapBundle(),
        );
    }
  3. app/config/routing.yml中注册路由(如果使用控制台命令预先生成网站地图,此步骤为可选)

    DpnXmlSitemapBundle:
        resource: "@DpnXmlSitemapBundle/Resources/config/routing.xml"

用法

公开路线

要将路线公开到网站地图,请将选项sitemap添加到您的路由定义中

blog_index:
    path:      /blog
    defaults:  { _controller: AppBundle:Blog:index }
    options:
        sitemap: true

这将使用您的配置中的默认选项公开此路线到网站地图。要控制此网站地图条目的选项,请将它们添加到sitemap选项中

blog_index:
    path:      /blog
    defaults:  { _controller: AppBundle:Blog:index }
    options:
        sitemap:
                priority: 0.7
                changefreq: hourly

注意:只能以这种方式公开没有参数的路线。对于有参数的路线,您必须创建一个自定义生成器(见下文)。

自定义生成器

对于具有参数的更复杂的路线,您必须创建一个自定义生成器。

  1. 创建一个实现Dpn\XmlSitemapBundle\Sitemap\GeneratorInterface的生成器类。此类必须有一个返回Dpn\XmlSitemapBundle\Sitemap\Entry对象数组的generate()方法。

    use Dpn\XmlSitemapBundle\Sitemap\Entry;
    use Dpn\XmlSitemapBundle\Sitemap\GeneratorInterface;
    
    class MySitemapGenerator implements GeneratorInterface
    {
        public function generate()
        {
            $entries = array();
    
            $entries[] = new Entry('http://example.com/foobar'); // must be absolute URL
    
            // add more entries - perhaps fetched from database
    
            return $entries;
        }
    }
  2. 将此类添加为带有dpn_xml_sitemap.generator标记的服务

    services:
        my_sitemap_generator:
            class: MySitemapGenerator
            tags:
                - { name: dpn_xml_sitemap.generator }

网站地图索引

根据sitemaps.orgsitemap.xml中可能有的条目最大数量为50,000。当网站地图条目数量超过此数量时,条目将分布在多个网站地图中(即/sitemap1.xml/sitemap2.xml.../sitemapN.xml)。

网站地图索引可在/sitemap_index.xml中访问。

每个网站地图的最大条目数可配置

dpn_xml_sitemap:
    max_per_sitemap: 50000 #default

HTTP缓存

您可以通过在配置中设置秒数来为sitemap(n).xml/sitemap_index.xml URI启用http缓存

dpn_xml_sitemap:
    http_cache: 3600

控制台转储命令

可用dpn:xml-sitemap:dump命令预先生成sitemap.xml文件

Usage:
 dpn:xml-sitemap:dump [--target="..."] host

Arguments:
 host      The full hostname for your website (ie http://www.google.com)

Options:
 --target  Override the target directory to dump sitemap(s) in

Help:
 Dumps your sitemap(s) to the filesystem (defaults to web/)

注意:此命令需要Symfony 2.4+。

完整默认配置

以下是此捆绑包的默认配置

dpn_xml_sitemap:

    # The length of time (in seconds) to cache the sitemap/sitemap_index xml\'s (a reverse proxy is required)
    http_cache:      ~

    # The number of url entries in a single sitemap
    max_per_sitemap: 50000

    # The default options for sitemap URL entries to be used if not overridden
    defaults:

        # Value between 0.0 and 1.0 or null for sitemap protocol default
        priority:   ~

        # One of [always, hourly, daily, weekly, monthly, yearly, never] or null for sitemap protocol default
        changefreq: ~

许可证

Resources/meta/LICENSE