tags:

views:

49

answers:

2

Using Quora has made me wonder how they do their slug like thes : quora.com/topics-slugs , quora.com/questions-slug or quora.com/usernames-slug.

Actually i am developing an application with yii framework and i want to have a slugs like quora does?

Thanks guys

A: 
  1. Handle any special characters you wanna use (Umlauts for example)
  2. Remove any non-alpha-numeric-characters that are left
  3. handle white spaces

Something like that for example:

function _getSlugFromName($name){
   return preg_replace('#[\s]+#','-',preg_replace('#[^\d\w -]*#','',str_replace(array('ä','ü','ö','ß'),array('ae','ue','oe','ss'),html_entity_decode(mb_strtolower(trim($name),'UTF-8'),ENT_COMPAT,'UTF-8'))));
}
Hannes
Thank u, But Maybe i didn't explain what i mean very well. what i meant is in quora.com you will see all the page including (topic, question and user name) has slug after the domain and there is no param infront of the slug to call the controller and action examples for topic http://www.quora.com/StackOverflow and for question http://www.quora.com/Why-doesnt-Quora-implement-a-reputation-system-a-la-StackOverflow so how does request proceed ?
tayler
A: 

You can also do this with rewrite rules. A rule like this in your config file:

'controller/<slug:[\w\-]+>'=>'controller/view',

will take a URL like this:

controller/my-slug

and it will redirect to the actionView() in your Controller, and pass the slug ("my-slug") in as a $_GET variable. With that rewrite rule you now call $_GET['slug'] and it will return "my-slug" from the url.

I have a "slug" row as a primary key so then I just query the DB for $_GET['slug'] in my actionView() and I get the correct record based on the URL. Works like a charm. Good luck!

UPDATE To get rid of the controller prefix in addition to using a slug, you will probably need one large table to keep track of all url slugs (to prevent duplicates). If you have that, then you could do a couple of different things:

1 Override onBeginRequest to do a lookup on the master slug table to figure out which Controller to call. 2. Use a master rewrite to a single ActionIndex in the SiteController, and in that action look up the slug in the master table to figure out which controller/action to send the user to. The rewrite rule would look something like this:

'<slug:[\w\-]+>'=>'site/index',
thaddeusmt
Thanks for help , But Maybe i didn't explain what i mean very well. what i meant is in quora.com you will see all the page including (topic, question and user name) has slug after the domain and there is no param infront of the slug to call the controller and action examples for topic http://www.quora.com/StackOverflow and for question http://www.quora.com/Why-doesnt-Quora-implement-a-reputation-system-a-la-StackOverflow so how does request proceed ?
tayler
I tried to add a little more info to point you in the right direction. Good luck!
thaddeusmt