edk
(Edwardkoo)
1
Hi,
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
localhost/yiisample/people/1750
how do i make the url to something like
localhost/people/SEATTLE/WA/John
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
array('id'=>$data->Id)
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
http://www.yiiframework.com/doc/guide/1.1/en/basics.controller#action
and this
http://www.yiiframework.com/doc/guide/1.1/en/topics.url
edk
(Edwardkoo)
3
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
localhost/yiisample/en/people/view/state/SEATTLE/city/WA/peoplename/John/peopleid/1750
i need it to be localhost/people/SEATTLE/WA/John
my main.php urlmanager section, do i need to modify this? how?
'urlManager'=>array
(
'class'=>'application.components.UrlManager',
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array
(
'<language:(en|zh)>/' => 'site/index',
'<language:(en|zh)>/<action:(contact|login|logout)>/*' => 'site/<action>',
'<language:(en|zh)>/<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<language:(en|zh)>/<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<language:(en|zh)>/<controller:\w+>/<action:\w+>/*'=>'<controller>/<action>',
),
),
proditis
(Proditis)
4
Hi,
Maybe you need something like the following into your urlManager rules.
'people/<state>/<city>/<peoplename>/<peopleid>' => 'people/view',
edk
(Edwardkoo)
5
Tried
-
‘people/<state>/<city>/<peoplename>/<peopleid>’ => ‘people/view’,
-
‘people/view/<state>/<city>/<peoplename>/<peopleid>’ => ‘people/view’,
-
‘<language:(en|zh)>/people/<state>/<city>/<peoplename>/<peopleid>’ => ‘people/view’,
-
‘<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.
edk
(Edwardkoo)
8
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.
edk
(Edwardkoo)
10
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));
zaccaria
(Matteo Falsitta)
12
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?
edk
(Edwardkoo)
13
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
localhost/yiisample/en/people/view/state/SEATTLE/city/WA/peoplename/John/peopleid/1750
from
Yii::app()->createUrl(‘people/view’, array(‘language’ => ‘en’, ‘state’=>$data->State, ‘city’=>$data->City, ‘peoplename’=>$data->Name, ‘peopleid’=>$data->Id));
or
Yii::app()->createUrl(‘people/view’, ‘state’=>$data->State, ‘city’=>$data->City, ‘peoplename’=>$data->Name, ‘peopleid’=>$data->Id));
I’m using this widget atm
http://www.yiiframework.com/wiki/294/seo-conform-multilingual-urls-language-selector-widget-i18n/
Probably it affected… hmm…
zaccaria
(Matteo Falsitta)
14
I miss the column:
'<language:(en|zh)>/people/<state:\w+>/<city:\w+>/<peoplename:\w+>' => 'people/view'
This one should work
edk
(Edwardkoo)
15
Still doesn’t work. Anyway, appreciate your advice.
chennaiiq
(Chennaiiq)
16
Can you show me current code in url_manager, controller, view…