dev-master
9999999-dev https://github.com/ddd-php/Slug
MIT
The Requires
- php >=5.3.0
by Joseph ROUFF
slug ddd
Slug is a component which generates slugs easily whatever persistence mecanism you use (Propel2, Doctrine2, custom ORM...)., (*1)
To generate a slug of a string there is always two 2 steps:, (*2)
Therefore Slug component has 2 services: TransliteratorInterface
and SlugGeneratorInterface
. Each of these services
can have multiple implementations:, (*3)
LatinTransliterator
: Transliterate a string written in any Latin
alphabet (French, Deutsh, Spanish...) into its ASCII equivalent.DefaultSlugGenerator
: Customize the word and field separator.PatternSlugGenerator
: Complete customization of slug generation.Using Composer, just require the ddd/components
package:, (*4)
``` javascript { "require": { "ddd/components": "dev-master" } }, (*5)
Usage ----- To be able to slugify an entity or model, you just have to implement the `SluggableInterface`: ``` php <?php use Ddd\Slug\Model\SluggableInterface; use Ddd\Slug\Service\SlugGeneratorInterface; class Article implements SluggableInterface { private $createdAt; private $title; private $slug; public function slugify(SlugGeneratorInterface $slugifier) { $this->slug = $slugifier->slugify(array($this->createdAt->format('Y'), $this->title)); } // other methods... }
Then you just have to call the slugify
method to generate the slug:, (*6)
``` php use Ddd\Slug\Infra\SlugGenerator\DefaultSlugGenerator; use Ddd\Slug\Infra\Transliterator\LatinTransliterator;, (*7)
$article = new Article(); $article->setTitle('Hello world!'); $article->slugify(new DefaultSlugGenerator(array(new LatinTransliterator())));, (*8)
echo $article->getSlug(); // writes "2013-hello-world" ```, (*9)
MIT
slug ddd