定制 charcoal/contrib-sitemap 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

charcoal/contrib-sitemap

Composer 安装命令:

composer require charcoal/contrib-sitemap

包简介

Charcoal service for generating a sitemap.

README 文档

README

License Build Status Latest Release Supported PHP Version

A Charcoal package for generating a sitemap.

Installation

composer require charcoal/contrib-sitemap

Setup

For Charcoal projects, the package can be registered in two ways.

Charcoal Module

The Sitemap's module will register the service provider (see below) and the route (/sitemap.xml).

The module can be registered from your configuration file:

"modules": {
    "charcoal/sitemap/sitemap": {}
}

Charcoal Service Provider

The Sitemap's service provider will register the necessary services (see below) for building a sitemap.

The service provider can be registered from your configuration file:

{
    "service_providers": {
        "charcoal/view/service-provider/view": {}
    }
}

To register a route from your configuration file:

{
    "routes": {
        "actions": {
            "sitemap.xml": {
                "route": "/sitemap.xml",
                "methods": [ "GET" ],
                "controller": "charcoal/sitemap/action/sitemap",
                "action_data": {
                    "sitemap_ident": "xml"
                }
            }
        }
    }
}

By default, the action controller will look for a sitemap hierarchy named xml which can be changed via the sitemap_ident controller setting.

Overview

Routes

  • GET /sitemap.xml — A route assigned to Charcoal\Sitemap\Action\SitemapAction.
    Used to serve the XML document.

Services

  • charcoal/sitemap/builder — Instance of Charcoal\Sitemap\Service\Builder.
    Used to generate the collections of links from the configured models.
  • sitemap/formatter/xml — Instance of Charcoal\Sitemap\Service\XmlFormatter.
    Used to generate the XML from one or more collections of links from the Builder.
  • sitemap/presenter — Instance of Charcoal\Sitemap\Service\SitemapPresenter.
    Used to resolve model transformations.
  • sitemap/transformer/factory — Instance of Charcoal\Factory\GenericFactory (charcoal/factory).
    Used to resolve object transformers from object types.

Configuration

The Sitemap can be configured from the application configset under the sitemap key. You can setup which objects to be included and available translations (l10n).

Most options are renderable by objects using your application's chosen template syntax (Mustache used in examples below).

Default Options

{
    /**
     * The service's configuration point.
     */
    "sitemap": {
        /**
         * One or more groups to customize how objects should be processed.
         *
         * The array key is an arbitrary identifier for the grouping of models.
         */
        "<group-name>": {
            /**
             * Whether or not to include links to translations.
             *
             * - `true` — Multilingual. Include all translations
             *   (see `locales.languages`).
             * - `false` — Unilingual. Include only the default language
             *   (see `locales.default_language`).
             */
            "l10n": false,
            /**
             * The language to include a link to if group is unilingual.
             *
             * If `l10n` is `true`, this option is ignored.
             *
             * Defaults to the application's current language.
             */
            "locale": "<current-language>",
            /**
             * Whether or not to check if the routable object
             * has an active route (`RoutableInterface#isActiveRoute()`)
             *
             * - `true` — Include only routable objects with active routes.
             * - `false` — Ignore if a routable object's route is active.
             */
            "check_active_routes": false,
            /**
             * Whether or not to prepend relative URIs with
             * the application's base URI (see `base_url`).
             *
             * - `true` — Use only the object's URI (see `sitemap.*.objects.*.url`).
             * - `false` — Prepend the base URI if object's URI is relative.
             */
            "relative_urls": false,
            /**
             * The transformer to parse each model included in `objects`.
             *
             * Either a PHP FQCN or snake-case equivalent.
             */
            "transformer": "<class-string>",
            /**
             * Map of models to include in the sitemap.
             */
            "objects": {
                /**
                 * One or more models to customize and include in the sitemap.
                 *
                 * The array key must be the model's object type,
                 * like `app/model/foo-bar`, or fully-qualified name (FQN),
                 * like `App\Model\FooBar`.
                 */
                "<object-type>": {
                    /**
                     * The transformer to parse the object.
                     *
                     * Either a PHP FQCN or snake-case equivalent.
                     */
                    "transformer": "<class-string>",
                    /**
                     * The URI of the object for the `<loc>` element.
                     */
                    "url": "{{ url }}",
                    /**
                     * The name of the object. Can be used in a
                     * custom sitemap builder or XML generator.
                     */
                    "label": "{{ title }}",
                    /**
                     * Map of arbitrary object data that can be used
                     * in a custom sitemap builder or XML generator.
                     */
                    "data": {},
                    /**
                     * List or map of collection filters of which objects
                     * to include in the sitemap.
                     *
                     * ```json
                     * "<filter-name>": {
                     *     "property": "active",
                     *     "value": true
                     * }
                     * ```
                     */
                    "filters": [],
                    /**
                     * List or map of collection orders to sort the objects
                     * in the sitemap.
                     *
                     * ```json
                     * "<order-name>": {
                     *     "property": "position",
                     *     "direction": "ASC"
                     * }
                     * ```
                     */
                    "orders": [],
                    /**
                     * Map of models to include in the sitemap
                     * below this model.
                     *
                     * Practical to group related models.
                     */
                    "children": {
                        /**
                         * One or more models to customize and include in the sitemap.
                         */
                        "<object-type>": {
                            /**
                             * A constraint on the parent object to determine
                             * if the child model's objects should be included
                             * in the sitemap.
                             */
                            "condition": null
                        }
                    }
                }
            }
        }
    }
}

Each model can override the following options of their group: l10n, locale, check_active_routes, relative_urls.

Example

The example below, identified as footer_sitemap, is marked as multilingual using the l10n option which will include all translations.

{
    "sitemap": {
        "footer_sitemap": {
            "l10n": true,
            "check_active_routes": true,
            "relative_urls": false,
            "transformer": "charcoal/sitemap/transformer/routable",
            "objects": {
                "app/object/section": {
                    "transformer": "\\App\\Transformer\\Sitemap\\Section",
                    "label": "{{ title }}",
                    "url": "{{ url }}",
                    "filters": {
                        "active": {
                            "property": "active",
                            "value": true
                        }
                    },
                    "data": {
                        "id": "{{ id }}",
                        "metaTitle": "{{ metaTitle }}"
                    },
                    "children": {
                        "app/object/section-children": {
                            "condition": "{{ isAnObjectParent }}"
                        }
                    }
                }
            }
        }
    }
}

Usage

Using the builder

The builder returns only an array. You need to make your own converter if you need another format.

Given the settings above:

$builder = $container['charcoal/sitemap/builder'];
// 'footer_sitemap' is the ident of the settings you want.
$links = $builder->build('footer_sitemap');

You can also use the SitemapBuilderAwareTrait, which includes the setter and getter for the sitemap builder, in order to use it with minimal code in every necessary class.

XML Formatter

The XML formatter generates a valid XML sitemap from the array returned by the builder.

$builder = $container['charcoal/sitemap/builder'];
$links   = $builder->build('footer_sitemap');

$formatter = $container['sitemap/formatter/xml'];
$sitemap   = $formatter->createXmlFromCollections($links);

Development

To install the development environment:

composer install

To run the scripts (PHP lint, PHPCS, PHPStan, and PHPUnit):

composer lint
composer test

License

Charcoal is licensed under the MIT license. See LICENSE for details.

charcoal/contrib-sitemap 适用场景与选型建议

charcoal/contrib-sitemap 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 32 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 03 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「Sitemap」 「charcoal」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 charcoal/contrib-sitemap 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 charcoal/contrib-sitemap 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-23