locomotivemtl / charcoal-contrib-sitemap
Charcoal service for generating a sitemap.
Installs: 1 461
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 14
Forks: 0
Open Issues: 0
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
A Charcoal service for generating a sitemap.
Table of Contents
Installation
The preferred (and only supported) method is with Composer:
$ composer require locomotivemtl/charcoal-contrib-sitemap
Dependencies
Required
- PHP 5.6+: PHP 7 is recommended.
- 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
Service Provider
Services
- charcoal/sitemap/builder Instance of
Charcoal\Sitemap\Service\Builder
. Used to generate collections of links from the configured models.
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 conversation if you need another format.
The Sitemap module will include all necessary service providers and set the route (sitemap.xml) directly. Include the module:
"modules": { "charcoal/sitemap/sitemap": {} }
Given the settings above:
$builder = $container['charcoal/sitemap/builder']; $sitemap = $builder->build('footer_sitemap'); // footer_sitemap is the ident of the settings you want.
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.
Sitemap.xml
This contrib provides a route for sitemap.xml
that dynamically loads the xml
config and outputs it
as an XML for crawlers to read.
Development
To install the development environment:
$ composer install
To run the scripts (phplint, phpcs, and phpunit):
$ composer test
API Documentation
- The auto-generated
phpDocumentor
API documentation is available at:
https://locomotivemtl.github.io/charcoal-contrib-sitemap/docs/master/ - The auto-generated
apigen
API documentation is available at:
https://codedoc.pub/locomotivemtl/charcoal-contrib-sitemap/master/
Development Dependencies
Coding Style
The charcoal-contrib-sitemap module follows the Charcoal coding-style:
- PSR-1
- PSR-2
- PSR-4, autoloading is therefore provided by Composer.
- phpDocumentor comments.
- phpcs.xml.dist and .editorconfig for coding standards.
Coding style validation / enforcement can be performed with
composer phpcs
. An auto-fixer is also available withcomposer phpcbf
.
Credits
License
Charcoal is licensed under the MIT license. See LICENSE for details.