Skip to content

phil/slug-bundle

alpha

Named sluggers declared in the configuration, autowired by name and usable in Twig.

phil/slug-bundle brings phil/slug into a Symfony application. The configuration names the sluggers an application needs, one per use, each with its locale, its longest slug and the leading words it drops. Each named slugger is a service of its own, so the application decorates it like any Symfony service, and the decorator takes its place everywhere. The default slugger autowires as Slugger, every named one as Slugger $<name>Slugger, and Sluggers holds them all by name. With Twig installed, the phil_slug filter makes a slug in a template. With the history enabled, the former slugs of each subject point to its current slug, so an old address can answer with a redirect.

Use it when a Symfony application makes slugs in more than one way, or wants the same slugger in its services and its templates: titles cut at 80 characters, directory names without their leading article. Outside Symfony, phil/slug alone builds the same sluggers by hand. No slugger drops words unless the configuration says so, since the articles of a language and a site's own prefixes are each application's choice.

The bundle holds no slug logic of its own. It turns each configured slugger into a TransliteratingSlugger, wrapped in a LeadingWordsRemoved when it lists leading words, and registers the aliases that make autowiring by argument name work.

Status

PartState
Named sluggers from the configuration, Sluggers, autowiring by nameImplemented and tested
Decoration of a named sluggerImplemented and tested
The phil_slug Twig filterImplemented and tested
The history of former slugs in a Doctrine DBAL table, optionalImplemented and tested

Documentation

DocumentWhat it covers
InstallationRequirements and registering.
ConfigurationThe sluggers, their options, the default one.
ServicesThe service ids, autowiring by name, decorating a slugger.
TwigThe phil_slug filter.
History of former slugsThe optional table of former slugs, recording a change, reading it for redirects.
Extension pointsWhat an application implements, configures, replaces or relies on.
TestingHow the bundle is tested.
ReferenceEvery type of the bundle.

Quick start

# config/packages/phil_slug.yaml
phil_slug:
    sluggers:
        default: ~
        directory:
            leading_words: [le, la, les, l, the]
public function __construct(private Slugger $directorySlugger) {}

$this->directorySlugger->slug('Les Gardiens')->value;   // 'gardiens'
<li data-sort-name="{{ name|phil_slug('directory') }}">{{ name }}</li>

Requirements

PHP 8.5 or later, Symfony 8.2 or later, phil/slug; Twig for the filter; doctrine/dbal for the history.

Contributing

Contributions are welcome on the PHIL monorepo: read the contribution guide and the code of conduct. Report a vulnerability privately, as the security policy explains, never in a public issue.

License

MIT, © Philippe Gamache, doing business as Le Gnome Archiviste; a Parler Haut Interagir Librement project. See LICENSE.