承接 jantaodev/sitemap-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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:

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:

  1. via console
bin/console jantao_dev:sitemap:generate
  1. 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 jantaodev/sitemap-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 149
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-09-04