Symfony Doctrine Sluggable – rebuilding empty slugs

If you add slugs with Sluggable behaviour in the middle of your project you’ll have empty slugs for all old records. To fix it you need to save your models via symfony, slugs will be built automatically.

You can automate it with help of this simple task:
(more…)

Wednesday, August 25th, 2010

Doctrine Sluggable and translit

Doctrine Sluggable behaviour doesn’t work with non-ASCII characters – it just removes them from the slug. For some languages, e.g. Russian with Cyrillic alphabet, it becomes unusable. The obvious way is to use transliteration.
Luckily Doctrine has easy way to do that, you can pass method for building slug:

Artist:
  actAs:
    Sluggable:
      fields: [name]
      builder: [oxtSluggableTranslit, urlize]

The easiest way I found to transliterate non-ASCII text is to use PECL translit package. For Debian-based systems you can install it with dh-make-php.
Here’s the code:

class oxtSluggableTranslit
{
  /**
   * Convert any passed string to a url friendly string. Converts 'My first blog post' to 'my-first-blog-post'
   *
   * @param  string $text  Text to urlize
   * @return string $text  Urlized text
   */
  static public function urlize($text)
  {
    return Doctrine_Inflector::urlize(transliterate($text, array('cyrillic_transliterate', 'remove_punctuation'), 'utf-8', 'utf-8'));
  }
}

You can view all available transliterate filters by running

php -r 'print_r(transliterate_filters_get());'

Monday, February 1st, 2010