dpn / xml-sitemap-bundle
通过从网站的路线中提取网站地图信息,为您的首选搜索引擎生成XML网站地图。
Requires
- symfony/asset: ^2.7|^3.0
- symfony/dependency-injection: ^2.7|^3.0
- symfony/framework-bundle: ^2.7|^3.0
- symfony/templating: ^2.7|^3.0
- symfony/twig-bundle: ^2.7|^3.0
Requires (Dev)
- matthiasnoback/symfony-dependency-injection-test: ^0.7.4
- mockery/mockery: ^0.9
- symfony/browser-kit: ^2.7|^3.0
- symfony/console: ^2.7|^3.0
- symfony/finder: ^2.7|^3.0
- symfony/yaml: ^2.7|^3.0
README
此捆绑包通过从应用程序的路线中提取网站地图信息为您的首选搜索引擎生成XML网站地图。此外,您还可以创建自己的生成器以提供URL。生成的网站地图遵循网站地图协议。
安装
-
使用composer安装
composer require dpn/xml-sitemap-bundle
-
在内核中启用捆绑包
// app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Dpn\XmlSitemapBundle\DpnXmlSitemapBundle(), ); }
-
在
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
注意:只能以这种方式公开没有参数的路线。对于有参数的路线,您必须创建一个自定义生成器(见下文)。
自定义生成器
对于具有参数的更复杂的路线,您必须创建一个自定义生成器。
-
创建一个实现
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; } }
-
将此类添加为带有
dpn_xml_sitemap.generator
标记的服务services: my_sitemap_generator: class: MySitemapGenerator tags: - { name: dpn_xml_sitemap.generator }
网站地图索引
根据sitemaps.org,sitemap.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
。