phpsymfony1symfony-1.4cyrillic

Symfony cyrillic routing slug


I have problem whit slugify routing parameter. I want to replace all intervals and symbols with "-". When parameter is with latin letters all work, but if I try to slugify parameter whit cyrillic letters I get error.

routing:

 catTests:
      url:    /cat/:id/:name_slug
      class:   sfDoctrineRoute
      options: { model: categories, type: object }
      param: { module: categories, action: testsByCat }
      requirements:
        id: \d+

slug functions:

static public function slugify($text)
{
  // replace all non letters or digits by -
  $text = preg_replace('/\W+/', '-', $text);

  // trim and lowercase
  $text = strtolower(trim($text, '-'));

  return $text;
}

public function getNameSlug()
{
  $text= Category::slugify($this->getName());
  return $text;
}

Example: i have two names in databace:

Normally whitin function url is :

When i put function result is :


Solution

  • I recommend you to use Doctrine::urlize instead of your own slugify function (since your are using Doctrine).

    And then, replace your function like:

    public function getNameSlug()
    {
      return Doctrine_Inflector::urlize($this->getName());
    }
    

    edit:

    In fact, it seems that Doctrine doesn't well handle Cyrillic (even in the 2.0). You will have to handle it on your own. I found this function:

      public static function replaceCyrillic ($text)
      {
        $chars = array(
          'ґ'=>'g','ё'=>'e','є'=>'e','ї'=>'i','і'=>'i',
          'а'=>'a', 'б'=>'b', 'в'=>'v',
          'г'=>'g', 'д'=>'d', 'е'=>'e', 'ё'=>'e',
          'ж'=>'zh', 'з'=>'z', 'и'=>'i', 'й'=>'i',
          'к'=>'k', 'л'=>'l', 'м'=>'m', 'н'=>'n',
          'о'=>'o', 'п'=>'p', 'р'=>'r', 'с'=>'s',
          'т'=>'t', 'у'=>'u', 'ф'=>'f', 'х'=>'h',
          'ц'=>'c', 'ч'=>'ch', 'ш'=>'sh', 'щ'=>'sch',
          'ы'=>'y', 'э'=>'e', 'ю'=>'u', 'я'=>'ya', 'é'=>'e', '&'=>'and',
          'ь'=>'', 'ъ' => '',
        );
    
        return strtr($text, $chars);
      }
    

    And then :

    public function getNameSlug()
    {
      $slug = Category::replaceCyrillic($this->getName());
      return Doctrine_Inflector::urlize($slug);
    }