How Do I Make Friendly Url Like This?


I have a view generated by gii

public function actionView($id)

i generate a url by

Yii::app()->createUrl('people/view', array('id'=>$data->Id));

what i got from above is


how do i make the url to something like


so first i need to get rid of yiisample from the url

then i need to include city/state/name in the url

In brief, you should change


to something like

array('state' => $data->state->name, 'user' => $data->name)

(of course, it depends on your actual data structure),

Then bind there params to action

actionView($state, $user)

(don’t forget to modify action code for new search conditions)

Then adjust your routing rules to prettify your url.

For details read this

and this

Ok i have change my actionView to

public function actionView($state, $city, $peoplename, $peopleid)

notice i still need parameter $peopleid to get the record from db

now the url has become


i need it to be localhost/people/SEATTLE/WA/John

my main.php urlmanager section, do i need to modify this? how?








				'<language:(en|zh)>/' => 'site/index',

				'<language:(en|zh)>/<action:(contact|login|logout)>/*' => 'site/<action>',







Maybe you need something like the following into your urlManager rules.

'people/<state>/<city>/<peoplename>/<peopleid>' => 'people/view',


  1. ‘people/<state>/<city>/<peoplename>/<peopleid>’ => ‘people/view’,

  2. ‘people/view/<state>/<city>/<peoplename>/<peopleid>’ => ‘people/view’,

  3. ‘<language:(en|zh)>/people/<state>/<city>/<peoplename>/<peopleid>’ => ‘people/view’,

  4. ‘<language:(en|zh)>/people/view/<state>/<city>/<peoplename>/<peopleid>’ => ‘people/view’,

All has no effect as createUrl still return same url :(

I think you need to create your own URL manager.

As I’ve mentioned before, you should pass all the named params of the route to the createUrl, otherwise some other route will be generated.

That is, for reverse routing of ‘people/<state>/<city>/<peoplename>/<peopleid>’ you should do something like this:

createUrl('people/view', 'state' => '...', 'city' => '...', 'peoplename' => '...', 'peopleid' => '...');

I bet you forgot that.

I did that, this is my code

Yii::app()->createUrl('people/view', array('state'=>$data->State, 'city'=>$data->City, 'peoplename'=>$data->Name, 'peopleid'=>$data->Id));

Still, what i got is localhost/yiisample/en/people/view/state/SEATTLE/city/WA/peoplename/John/peopleid/1750

So everything looks fine, except the ‘yiisample’ part. But this is the path from your webroot to yii app, right?

You should move app directly to webroot to get rid of this.

No, what i got now is localhost/yiisample/en/people/view/state/SEATTLE/city/WA/peoplename/John/peopleid/1750

What i want is localhost/en/people/SEATTLE/WA/John

My yii app is already sits at webroot/yiisample. Do you mean moving everything inside yiisample to webroot?

Yes, and the only route you need is

 '<language:(en|zh)>/people/<state>/<city>/<peoplename>' => 'people/view'

and for reverse routing you do

Yii::app()->createUrl('people/view', array('language' => 'en', 'state'=>$data->State, 'city'=>$data->City, 'peoplename'=>$data->Name));

The rules should be configured like that:

 '<language:(en|zh)>/people/<state:\w+>/<city\w+>/<peoplename\w+>' => 'people/view'

About the person id, you must include it in the url if you want to receive as parameter, how is supposed to guess the id elseway?

I tried

‘<language:(en|zh)>/people/<state>/<city>/<peoplename>’ => ‘people/view’

‘<language:(en|zh)>/people/<state:\w+>/<city\w+>/<peoplename\w+>’ => ‘people/view’

‘<language:(en|zh)>/people/<state:\w+>/<city\w+>/<peoplename\w+>/<peopleid\w+>’ => ‘people/view’

All doesn’t work, i still get



Yii::app()->createUrl(‘people/view’, array(‘language’ => ‘en’, ‘state’=>$data->State, ‘city’=>$data->City, ‘peoplename’=>$data->Name, ‘peopleid’=>$data->Id));


Yii::app()->createUrl(‘people/view’, ‘state’=>$data->State, ‘city’=>$data->City, ‘peoplename’=>$data->Name, ‘peopleid’=>$data->Id));

I’m using this widget atm

Probably it affected… hmm…

I miss the column:

'<language:(en|zh)>/people/<state:\w+>/<city:\w+>/<peoplename:\w+>' => 'people/view'

This one should work

Still doesn’t work. Anyway, appreciate your advice.

Can you show me current code in url_manager, controller, view…