chamber-orchestra/doctrine-slug-bundle
最新稳定版本:v8.0.1
Composer 安装命令:
composer require chamber-orchestra/doctrine-slug-bundle
包简介
The symfony doctrine slug bundle
README 文档
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.
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04