Slugs

Hello everyone,

I am sorry to ask yet another URL Management related question - couldn’t find the answer to my question anywhere though. I am pretty new to the Yii Framework but I think I’ve got the concept of routes and the path format for user friendly URLs. But I am not sure whats the best way to validate user generated slugs (eg. usernames ala domain.com/username) to protect pre-defined routes.

How do I make sure a slug is not part of a module, controller or action name or breaking any other URL Manager rules?

Thanks,

D

Hi and welcome to the forum.

Why not do: http://example.com/user/dmx or http://example.com/profile/dmx

Descriptive url’s are good.

However, take a look at the range validator in combination with not property. You can pass all "forbidden" slugs as array and disallow user registration for those.

Though it may be hard to get all possible values. Also what happens if you want to add a section to you site - maybe a user has a slug that matches the section name? I suggest to simply use descriptive url’s.

I had a similar problem in a recent work.

Our seo expert was complaining that "doing lot of folders will result in seo depenalization", so I arranged to do url like that:




http://example.com/user_dmx



Wich can be easily achived with this rule:




'user_<id:[\w-]+>'=>'user/show',



The addres is the same, only with underscore instead of the shlas, so our seo expert is happy, and there are no possibility of slug conflicts.

Thank you for your fast replies and you are right about descriptive urls, but super-short profile urls are kind of a must-have for community sites these days (see facebook, twitter or myspace). I’ll try out your suggestion but I really was looking for some sort of routeExists() method. Any ideas?

Okay, maybe some custom method like this (untested)?




class UrlManager extends CUrlManager

{


   public function getDirectories()

   {


      $directories = array();


      foreach ($this->rules as $pattern => $route)

      {

         if (preg_match("/^(?<directory>[^<]+?)\/?/", $pattern, $match) && !in_array($match['directory'], $directories))

         {

            $directories[] = $match['directory'];

         }

      }


      return $directories;


   }


}



In case you have the following rules:




'signup' => '...',

'login' => '...',

'some/other/section' => '...',

'<userSlug:.+>' => '...',



The returned array should contain:


signup

login

some

You can use the returned array with range validator. Of course you can also rewrite the method to return a boolean.

This may help you: dburlmanager