locomotivemtl / charcoal-contrib-sitemap
用于生成网站地图的Charcoal服务。
0.4.1
2024-06-06 19:10 UTC
Requires
- php: >=5.6.0 || >=7.0
- locomotivemtl/charcoal-app: ~0.8
- locomotivemtl/charcoal-core: ~0.3
- locomotivemtl/charcoal-factory: ~0.4
- locomotivemtl/charcoal-object: ~0.4
- locomotivemtl/charcoal-translator: ~0.3
- locomotivemtl/charcoal-view: ~0.3
Requires (Dev)
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: ^5.7 || ^6.5
- squizlabs/php_codesniffer: ^3.0
README
一个用于生成网站地图的Charcoal服务。
目录
安装
首选(也是唯一支持)的方法是使用Composer
$ composer require locomotivemtl/charcoal-contrib-sitemap
依赖项
要求
- PHP 5.6+: PHP 7 推荐使用。
- locomotivemtl/charcoal-app: ^0.8
- locomotivemtl/charcoal-core: ^0.3
- locomotivemtl/charcoal-factory: ^0.4
- locomotivemtl/charcoal-object: ^0.4
- locomotivemtl/charcoal-translator: ^0.3
服务提供商
服务
- charcoal/sitemap/builder 实例化
Charcoal\Sitemap\Service\Builder。用于从配置的模型生成链接集合。
配置
可以从应用程序配置文件中的 sitemap 键配置Sitemap。您可以设置要包含的对象和可用的翻译(l10n)。
大多数选项都可以使用应用程序选择的模板语法(以下示例中使用Mustache)由对象渲染。
默认选项
{ /** * 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 } } } } } } }
每个模型可以覆盖其组中的以下选项: l10n、locale、check_active_routes、relative_urls。
示例
以下示例,标识为 footer_sitemap,使用 l10n 选项标记为多语言,这将包含所有翻译。
{
"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 }}"
}
}
}
}
}
}
}
使用方法
使用构建器
构建器仅返回数组。如果您需要其他格式,则需要自己进行转换。
Sitemap模块将包含所有必要的服务提供商并直接设置路由(sitemap.xml)。包含模块
"modules": { "charcoal/sitemap/sitemap": {} }
根据上述设置
$builder = $container['charcoal/sitemap/builder']; $sitemap = $builder->build('footer_sitemap'); // footer_sitemap is the ident of the settings you want.
您还可以使用 SitemapBuilderAwareTrait,它包括对sitemap构建器的设置器和获取器,以便在所有必要的类中使用最小代码。
Sitemap.xml
此贡献提供对 sitemap.xml 的路由,它动态加载 xml 配置并将其作为XML输出供爬虫读取。
开发
要安装开发环境
$ composer install
要运行脚本(phplint、phpcs和phpunit)
$ composer test
API文档
- 自动生成的
phpDocumentorAPI文档可在以下位置获取
https://locomotivemtl.github.io/charcoal-contrib-sitemap/docs/master/ - 自动生成的
apigenAPI文档可在以下位置获取
https://codedoc.pub/locomotivemtl/charcoal-contrib-sitemap/master/
开发依赖项
编码风格
charcoal-contrib-sitemap模块遵循Charcoal编码风格
- PSR-1
- PSR-2
- PSR-4,因此自动加载由 Composer 提供。
- phpDocumentor注释。
- phpcs.xml.dist 和 .editorconfig 用于编码标准。
可以使用
composer phpcs执行编码风格验证/强制执行。还有可用的自动修复器,带有composer phpcbf。
鸣谢
许可证
Charcoal遵循MIT许可证。有关详细信息,请参阅LICENSE。