chamber-orchestra / doctrine-slug-bundle
The symfony doctrine slug bundle
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/chamber-orchestra/doctrine-slug-bundle
Requires
- php: ^8.4
- chamber-orchestra/metadata-bundle: 8.0.*
- symfony/string: 8.0.*
- symfony/translation-contracts: ^3.0
Requires (Dev)
- phpunit/phpunit: ^12.5
- symfony/test-pack: ^1.2
Conflicts
README
Doctrine Slug Bundle
Symfony bundle that generates unique, URL-friendly slugs for Doctrine ORM entities using PHP 8 attributes. It integrates with Chamber Orchestra metadata and Doctrine listeners to keep slugs consistent on persist and (optionally) on update.
Installation
composer require chamber-orchestra/doctrine-slug-bundle
If Symfony Flex does not auto-register the bundle, add it manually:
// config/bundles.php return [ ChamberOrchestra\DoctrineSlugBundle\ChamberOrchestraDoctrineSlugBundle::class => ['all' => true], ];
Dependencies
Runtime requirements are managed by Composer. The bundle depends on:
- PHP 8.4+
chamber-orchestra/metadata-bundlesymfony/stringsymfony/translation-contracts- Doctrine ORM + DoctrineBundle (for entity listeners)
Usage
Annotate a sluggable property with the #[Slug] attribute and ensure the slug column is unique.
namespace App\Entity; use ChamberOrchestra\DoctrineSlugBundle\Mapping\Attribute\Slug; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] class Post { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(type: 'integer')] private ?int $id = null; #[ORM\Column(type: 'string', length: 255)] private string $name = ''; #[ORM\Column(type: 'string', length: 255, unique: true)] #[Slug(source: 'name')] private string $slug = ''; public function getSlug(): string { return $this->slug; } }
Options:
source: source field name for slug generation.update: set totrueto regenerate slug when the source changes.separator: character used between words (default-).
Notes:
- The slug column must be
unique. - If the source column is nullable and the slug column is not, mapping will throw.
Using the SlugTrait
You can also reuse the provided trait:
use ChamberOrchestra\DoctrineSlugBundle\Entity\SlugTrait; class Post { use SlugTrait; }
The trait defines name and slug fields with proper Doctrine mapping and a #[Slug(source: 'name')] attribute.
Running Tests
composer test
This runs PHPUnit with the configuration in phpunit.xml.dist.