|
@@ -11,11 +11,15 @@ Features:
|
|
|
- Can be nested with other behaviors
|
|
|
- Annotation, Yaml and Xml mapping support for extensions
|
|
|
- Multiple slugs, diferent slugs can link to same fields
|
|
|
-- Built-in slug handlers, for tree path based slugs or linked by relation
|
|
|
|
|
|
[blog_reference]: http://gediminasm.org/article/sluggable-behavior-extension-for-doctrine-2 "Sluggable extension for Doctrine 2 makes automatic record field transformations into url friendly names"
|
|
|
[blog_test]: http://gediminasm.org/test "Test extensions on this blog"
|
|
|
|
|
|
+Update **2012-02-26**
|
|
|
+
|
|
|
+- Remove slug handlers were removed because of complications it brought together
|
|
|
+
|
|
|
+
|
|
|
Update **2011-09-11**
|
|
|
|
|
|
- Refactored sluggable for doctrine2.2 by specifieng slug fields directly in slug annotation
|
|
@@ -39,7 +43,7 @@ does not work on OSX 10.6 its ok starting again from 10.7 version. To overcome t
|
|
|
you can use your [custom transliterator](#transliterator)
|
|
|
- You can [test live][blog_test] on this blog
|
|
|
- Public [Sluggable repository](http://github.com/l3pp4rd/DoctrineExtensions "Sluggable extension on Github") is available on github
|
|
|
-- Last update date: **2012-02-04**
|
|
|
+- Last update date: **2012-02-26**
|
|
|
|
|
|
**Portability:**
|
|
|
|
|
@@ -82,7 +86,7 @@ on how to setup and use the extensions in most optimized way.
|
|
|
you need to identify entity as being Sluggable. The metadata is loaded only once then
|
|
|
cache is activated
|
|
|
|
|
|
-**Note:** 2.1.x version of extensions used @Gedmo\Mapping\Annotation\Sluggable to identify
|
|
|
+**Note:** 2.0.x version of extensions used @Gedmo\Mapping\Annotation\Sluggable to identify
|
|
|
the field for slug
|
|
|
|
|
|
``` php
|
|
@@ -316,65 +320,6 @@ echo $article->getSlug();
|
|
|
- **unique** (optional, default=true) - **true** if slug should be unique and if identical it will be prefixed, **false** - otherwise
|
|
|
- **separator** (optional, default="-") - separator which will separate words in slug
|
|
|
- **style** (optional, default="default") - **"default"** all letters will be lowercase, **"camel"** - first word letter will be uppercase
|
|
|
-- **handlers** (optional, default=[]) - list of slug handlers, like tree path slug, or customized, for example see bellow
|
|
|
-
|
|
|
-**Note**: handlers are totally optional
|
|
|
-
|
|
|
-**TreeSlugHandler**
|
|
|
-
|
|
|
-``` php
|
|
|
-<?php
|
|
|
-/**
|
|
|
- * @Gedmo\Mapping\Annotation\Slug(handlers={
|
|
|
- * @Gedmo\Mapping\Annotation\SlugHandler(class="Gedmo\Sluggable\Handler\TreeSlugHandler", options={
|
|
|
- * @Gedmo\Mapping\Annotation\SlugHandlerOption(name="parentRelationField", value="parent"),
|
|
|
- * @Gedmo\Mapping\Annotation\SlugHandlerOption(name="separator", value="/")
|
|
|
- * })
|
|
|
- * }, fields={"title", "code"})
|
|
|
- * @Doctrine\ORM\Mapping\Column(length=64, unique=true)
|
|
|
- */
|
|
|
-private $slug;
|
|
|
-```
|
|
|
-
|
|
|
-**RelativeSlugHandler**:
|
|
|
-
|
|
|
-``` php
|
|
|
-<?php
|
|
|
-/**
|
|
|
- * Person domain object class
|
|
|
- *
|
|
|
- * @Gedmo\Mapping\Annotation\Slug(handlers={
|
|
|
- * @Gedmo\Mapping\Annotation\SlugHandler(class="Gedmo\Sluggable\Handler\RelativeSlugHandler", options={
|
|
|
- * @Gedmo\Mapping\Annotation\SlugHandlerOption(name="relationField", value="category"),
|
|
|
- * @Gedmo\Mapping\Annotation\SlugHandlerOption(name="relationSlugField", value="slug"),
|
|
|
- * @Gedmo\Mapping\Annotation\SlugHandlerOption(name="separator", value="/")
|
|
|
- * })
|
|
|
- * }, fields={"title", "code"})
|
|
|
- * @Doctrine\ORM\Mapping\Column(length=64, unique=true)
|
|
|
- */
|
|
|
-private $slug;
|
|
|
-```
|
|
|
-
|
|
|
-**Note:** if you used **RelativeSlugHandler** - relation object should use in order to sync changes:
|
|
|
-
|
|
|
-**InversedRelativeSlugHandler**
|
|
|
-
|
|
|
-``` php
|
|
|
-<?php
|
|
|
-/**
|
|
|
- * Category domain object class
|
|
|
- *
|
|
|
- * @Gedmo\Mapping\Annotation\Slug(handlers={
|
|
|
- * @Gedmo\Mapping\Annotation\SlugHandler(class="Gedmo\Sluggable\Handler\InversedRelativeSlugHandler", options={
|
|
|
- * @Gedmo\Mapping\Annotation\SlugHandlerOption(name="relationClass", value="App\Entity\Person"),
|
|
|
- * @Gedmo\Mapping\Annotation\SlugHandlerOption(name="mappedBy", value="category"),
|
|
|
- * @Gedmo\Mapping\Annotation\SlugHandlerOption(name="inverseSlugField", value="slug")
|
|
|
- * })
|
|
|
- * }, fields={"title"})
|
|
|
- * @Doctrine\ORM\Mapping\Column(length=64, unique=true)
|
|
|
- */
|
|
|
-private $slug;
|
|
|
-```
|
|
|
|
|
|
### Example
|
|
|
|
|
@@ -548,175 +493,4 @@ class Article
|
|
|
|
|
|
Now the generated slug will be translated by Translatable behavior
|
|
|
|
|
|
-<a name="slug-handlers"></a>
|
|
|
-
|
|
|
-## Using slug handlers:
|
|
|
-
|
|
|
-There are built-in slug handlers like described in configuration options of slug, but there
|
|
|
-can be also customized slug handlers depending on use cases. Usually the most logic use case
|
|
|
-is for related slug. For instance if user has a **ManyToOne relation to a **Company** we
|
|
|
-would like to have a url like **http://example.com/knplabs/gedi where **KnpLabs**
|
|
|
-is a company and user name is **Gedi**. In this case relation has a path separator **/**
|
|
|
-
|
|
|
-User entity example:
|
|
|
-
|
|
|
-``` php
|
|
|
-<?php
|
|
|
-namespace Sluggable\Fixture\Handler;
|
|
|
-
|
|
|
-use Gedmo\Mapping\Annotation as Gedmo;
|
|
|
-use Doctrine\ORM\Mapping as ORM;
|
|
|
-
|
|
|
-/**
|
|
|
- * @ORM\Entity
|
|
|
- */
|
|
|
-class User
|
|
|
-{
|
|
|
- /**
|
|
|
- * @ORM\Id
|
|
|
- * @ORM\GeneratedValue
|
|
|
- * @ORM\Column(type="integer")
|
|
|
- */
|
|
|
- private $id;
|
|
|
-
|
|
|
- /**
|
|
|
- * @ORM\Column(length=64)
|
|
|
- */
|
|
|
- private $username;
|
|
|
-
|
|
|
- /**
|
|
|
- * @Gedmo\Slug(handlers={
|
|
|
- * @Gedmo\SlugHandler(class="Gedmo\Sluggable\Handler\RelativeSlugHandler", options={
|
|
|
- * @Gedmo\SlugHandlerOption(name="relationField", value="company"),
|
|
|
- * @Gedmo\SlugHandlerOption(name="relationSlugField", value="alias"),
|
|
|
- * @Gedmo\SlugHandlerOption(name="separator", value="/")
|
|
|
- * })
|
|
|
- * }, separator="-", updatable=true, fields={"username"})
|
|
|
- * @ORM\Column(length=64, unique=true)
|
|
|
- */
|
|
|
- private $slug;
|
|
|
-
|
|
|
- /**
|
|
|
- * @ORM\ManyToOne(targetEntity="Company")
|
|
|
- */
|
|
|
- private $company;
|
|
|
-
|
|
|
- public function setCompany(Company $company = null)
|
|
|
- {
|
|
|
- $this->company = $company;
|
|
|
- }
|
|
|
-
|
|
|
- public function getCompany()
|
|
|
- {
|
|
|
- return $this->company;
|
|
|
- }
|
|
|
-
|
|
|
- public function getId()
|
|
|
- {
|
|
|
- return $this->id;
|
|
|
- }
|
|
|
-
|
|
|
- public function setName($name)
|
|
|
- {
|
|
|
- $this->name = $name;
|
|
|
- }
|
|
|
-
|
|
|
- public function getName()
|
|
|
- {
|
|
|
- return $this->name;
|
|
|
- }
|
|
|
-
|
|
|
- public function getSlug()
|
|
|
- {
|
|
|
- return $this->slug;
|
|
|
- }
|
|
|
-}
|
|
|
-```
|
|
|
-
|
|
|
-Company entity example:
|
|
|
-
|
|
|
-``` php
|
|
|
-<?php
|
|
|
-namespace Sluggable\Fixture\Handler;
|
|
|
-
|
|
|
-use Gedmo\Mapping\Annotation as Gedmo;
|
|
|
-use Doctrine\ORM\Mapping as ORM;
|
|
|
-
|
|
|
-/**
|
|
|
- * @ORM\Entity
|
|
|
- */
|
|
|
-class Company
|
|
|
-{
|
|
|
- /**
|
|
|
- * @ORM\Id
|
|
|
- * @ORM\GeneratedValue
|
|
|
- * @ORM\Column(type="integer")
|
|
|
- */
|
|
|
- private $id;
|
|
|
-
|
|
|
- /**
|
|
|
- * @ORM\Column(length=64)
|
|
|
- */
|
|
|
- private $title;
|
|
|
-
|
|
|
- /**
|
|
|
- * @Gedmo\Slug(handlers={
|
|
|
- * @Gedmo\SlugHandler(class="Gedmo\Sluggable\Handler\InversedRelativeSlugHandler", options={
|
|
|
- * @Gedmo\SlugHandlerOption(name="relationClass", value="Sluggable\Fixture\Handler\User"),
|
|
|
- * @Gedmo\SlugHandlerOption(name="mappedBy", value="company"),
|
|
|
- * @Gedmo\SlugHandlerOption(name="inverseSlugField", value="slug")
|
|
|
- * })
|
|
|
- * }, fields={"title"})
|
|
|
- * @ORM\Column(length=64, unique=true)
|
|
|
- */
|
|
|
- private $alias;
|
|
|
-
|
|
|
- public function getId()
|
|
|
- {
|
|
|
- return $this->id;
|
|
|
- }
|
|
|
-
|
|
|
- public function setTitle($title)
|
|
|
- {
|
|
|
- $this->title = $title;
|
|
|
- }
|
|
|
-
|
|
|
- public function getTitle()
|
|
|
- {
|
|
|
- return $this->title;
|
|
|
- }
|
|
|
-
|
|
|
- public function getAlias()
|
|
|
- {
|
|
|
- return $this->alias;
|
|
|
- }
|
|
|
-}
|
|
|
-```
|
|
|
-
|
|
|
-And the example usage:
|
|
|
-
|
|
|
-``` php
|
|
|
-<?php
|
|
|
-$company = new Company;
|
|
|
-$company->setTitle('KnpLabs');
|
|
|
-$em->persist($company);
|
|
|
-
|
|
|
-$gedi = new User;
|
|
|
-$gedi->setUsername('Gedi');
|
|
|
-$gedi->setCompany($company);
|
|
|
-$em->persist($gedi);
|
|
|
-
|
|
|
-$em->flush();
|
|
|
-
|
|
|
-echo $gedi->getSlug(); // outputs "knplabs/gedi"
|
|
|
-
|
|
|
-$company->setTitle('KnpLabs Nantes');
|
|
|
-$em->persist($company);
|
|
|
-$em->flush();
|
|
|
-
|
|
|
-echo $gedi->getSlug(); // outputs "knplabs-nantes/gedi"
|
|
|
-```
|
|
|
-
|
|
|
-**Note:** tree slug handler, takes a parent relation to build slug recursively.
|
|
|
-
|
|
|
Any suggestions on improvements are very welcome
|