jantaodev/sitemap-bundle
Composer 安装命令:
composer require jantaodev/sitemap-bundle
包简介
Symfony bundle that provides sitemap XML generation
README 文档
README
This Symfony bundle provides sitemap XML generation.
Features:
- advanced robots.txt configuration
- several hosts support
- optional sitemap GZip compression
- sitemap constraints (10 MBytes/50 000 items per file) support
- route parameters iteration
Requirements:
- PHP 7.2
- Symfony 5
- Doctrine 2
1. Installation
Run composer require jantaodev/sitemap-bundle.
Register the bundle in the app/AppKernel.php file (optional):
... public function registerBundles() { $bundles = array( ... new JantaoDev\SitemapBundle\JantaoDevSitemapBundle(), ... ); ...
Add host name in app/config/parameters.yml
parameters: ... router.request_context.host: example.com router.request_context.scheme: http
or in app/config/config.yml
... jantao_dev_sitemap: hosts: - example.com
Now you can use the bundle.
2. Basic usage
There are several ways to add route to sitemap.
2.1. Annotation, yaml, XML (for static routes without parameters)
Annotation simple example:
/** * @Route("/", name="homepage", options={"sitemap" = true}) */
Yaml simple example:
homepage: path: / defaults: { _controller: "AppBundle:Default:index" } options: sitemap: true
XML simple example:
<route id="homepage" path="/"> <default key="_controller">AppBundle:Default:index</default> <option key="sitemap">true</option> </route>
Annotation full example:
/** * @Route("/", name="homepage", options={"priority" = 0.5, "changeFreq" = "monthly", "lastMod" = "2017-02-23T13:14:15+02:00" }) */
Yaml full example:
homepage: path: / defaults: { _controller: "AppBundle:Default:index" } options: sitemap: priority: 0.5 changeFreq: monthly lastMod: "2017-02-23T13:14:15+02:00"
XML full example:
<route id="homepage" path="/"> <default key="_controller">AppBundle:Default:index</default> <option key="sitemap"> {"priority":"0.5", "changeFreq":"monthly", "lastMod":"2017-02-23T13:14:15+02:00"} </option> </route>
There are three parameters:
- priority (real number from 0 to 1)
- changeFreq (one of values: always, never, hourly, daily, weekly, monthly, yearly)
- lastMod (see http://php.net/manual/en/datetime.formats.php)
It`s not necessary to specify all parameters.
2.2. Bundle configuration in app/config/config.yml
Simple example for static route:
jantao_dev_sitemap: ... sitemap: homepage: ~
Full example for static route:
jantao_dev_sitemap: ... sitemap: homepage: priority: 0.5 change_freq: monthly last_mod: "2017-02-23T13:14:15+02:00"
Full example for dynamic route with parameters:
jantao_dev_sitemap: ... sitemap: homepage: priority: 0.5 change_freq: monthly last_mod: "2017-02-23T13:14:15+02:00" route_parameters: slug: "someslug"
Iterate data for route parameters with array iterator:
jantao_dev_sitemap: ... sitemap: homepage: iterator: array values: - slugone - slugtwo - slugthree priority: 0.5 change_freq: monthly last_mod: "2017-02-23T13:14:15+02:00" route_parameters: slug: "->"
Iterate data for route parameters with array iterator (example 2):
jantao_dev_sitemap: ... sitemap: homepage: iterator: array values: - {slug: slugone, change: monthly} - {slug: slugtwo, change: hourly} - {slug: slugthree, change: dayly} priority: 0.5 change_freq: "->change" last_mod: "2017-02-23T13:14:15+02:00" route_parameters: slug: "->slug"
Iterate data for route parameters with Doctrine iterator:
jantao_dev_sitemap: ... sitemap: homepage: iterator: doctrine query: 'SELECT p FROM AppBundle:Page p WHERE p.enabled = true' priority: 0.5 change_freq: monthly last_mod: "->modifiedAt" route_parameters: slug: "->slug" categorySlug: "->category->slug"
3. Execute sitemap generation
There are two ways to execute sitemap generation:
- via console
bin/console jantao_dev:sitemap:generate
- via service
$this->get('jantao_dev.sitemap')->generate();
4. Advanced configuration
Full bundle configuration:
jantao_dev_sitemap: hosts: - example.com gzip: false scheme: https robots: # Robots.txt configuration sitemap: # Sitemap configuration
Parameters description:
| Parameter | Description |
|---|---|
| hosts | For one host configuration see chapter 1, For several hosts see chapter 4.2 |
| gzip | Enable sitemap GZip compression |
| scheme | Host scheme mode (http, https, https_only*) |
| robots | See chapter 4.1 |
| sitemap | See chapters 2.2 and 4.2 |
- https_only means that site is available only over https
4.1. Robots.txt configuration
Full robots.txt configuration:
jantao_dev_sitemap: ... robots: allow: "/ajax/": ~ "/system/": "Googlebot" disallow: "/admin/": ~ "/otherpath": "Googlebot" crawl_delay: 5 clean_param: "/photo": ["query=1", "page=2"]
Parameters description:
| Parameter | Description |
|---|---|
| allow | Allow entries array, key is a path, value is a user-agent (null means "*") |
| disallow | Disallow entries array |
| crawl_delay | Crawl delay parameter |
| clean_param | Clean param entries array, key is a path, value is an array of parameters |
For more details about robots.txt parameters see this or this.
4.2. Several hosts support
Several hosts support allows you to generate different sitemaps to several hosts.
So, enable several hosts support, list your hosts in bundle configuration:
jantao_dev_sitemap: ... hosts: example.com foo.com bar.com
Enable robots.txt switching controller in router (add following lines to app/config/routing.yml):
jantao_dev_sitemap: resource: "@JantaoDevSitemapBundle/Resources/config/routing.xml" prefix: /
By default all ruotes will be added to each host sitemaps.
But in sitemap section in bundle configuration you can specify host.
Example:
jantao_dev_sitemap: ... hosts: example.com foo.com bar.com ... sitemap: homepage: ~ about@foo.com: ~
In this example homepage will be added to all sitemaps, but about page will be add only to "foo.com" sitemap.
Example 2 (different slugs):
jantao_dev_sitemap: ... hosts: example.com ru.example.com de.example.com ... sitemap: homepage@example.com: iterator: doctrine query: 'SELECT p FROM AppBundle:Page p WHERE p.enabled = true' priority: 0.5 change_freq: monthly last_mod: "->modifiedAt" route_parameters: slug: "->slugEn" homepage@ru.example.com: iterator: doctrine query: 'SELECT p FROM AppBundle:Page p WHERE p.enabled = true' priority: 0.5 change_freq: monthly last_mod: "->modifiedAt" route_parameters: slug: "->slugRu" homepage@de.example.com: iterator: doctrine query: 'SELECT p FROM AppBundle:Page p WHERE p.enabled = true' priority: 0.5 change_freq: monthly last_mod: "->modifiedAt" route_parameters: slug: "->slugDe"
5. Add custom event listener
To add a URL using your own logic, you can use event listener or event subscriber.
Example AppBundle/EventListener/SomeListener.php:
<?php namespace AppBundle\EventListener; use JantaoDev\SitemapBundle\Service\SitemapListenerInterface; use JantaoDev\SitemapBundle\Event\SitemapGenerateEvent; use JantaoDev\SitemapBundle\Sitemap\Url; class SomeListener implements SitemapListenerInterface { public function generateSitemap(SitemapGenerateEvent $event) { $sitemap = $event->getSitemap(); $url = new Url('/index.php', new \DateTime('now'), 0.8, 'weekly'); $sitemap->add($url); } }
Example AppBundle/Resources/config/services.yml:
services: app.some_listener: class: AppBundle\EventListener\SomeListener tags: - { name: jantao_dev.sitemap.listener }
6. Notes
TODO:
- cover EventListener classes with tests
- add report on command
7. License
This bundle is under MIT license
jantaodev/sitemap-bundle 适用场景与选型建议
jantaodev/sitemap-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 149 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 09 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「Sitemap」 「robots」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jantaodev/sitemap-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jantaodev/sitemap-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jantaodev/sitemap-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Symfony sitemap generator
PHP Simple Sitemap Generator
The bundle for easy using json-rpc api on your project
Interfaces for web resource models and services to retrieve and create them
A simple plugin that adds a twig tag to obfuscate email addresses (by rot13) in text fields.
Sitemap crawler/generator. For the given URL it will return sitemap XML file with URLs and images.
统计信息
- 总下载量: 149
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-09-04