mech7
(Chris De Kok)
July 19, 2010, 11:02am
1
I have the following routes in my config…
'urlManager'=>array(
'showScriptName' => false,
'urlFormat' => 'path',
'rules' => array(
'book/<id:\d+>/*'=>'books/view',
'<lang:(th|en|jp)>/book/<id:\d+>/*'=>'books/view',
'<lang:(th|en|jp)>/<_c>/<_a>/*' => '<_c>/<_a>/',
'<lang:a-z{2}_a-z{2,}>/<_m>/<_c>/<_a>' => '<_m>/<_c>/<_a>',
'gii'=>'gii',
'gii/<controller:\w+>'=>'gii/<controller>',
'gii/<controller:\w+>/<action:\w+>'=>'gii/<controller>/<action>',
),
),
But if I use:
CHtml:link('test', array('create', 'lang' => 'th'));
It will add the lang parameter ad the end of the url instead the beginning… Normally with other rewrites this does work correctly So what is going wrong here?
mikl
(Mike)
July 19, 2010, 11:22am
2
You have no rule for the route ‘create’.
mech7
(Chris De Kok)
July 19, 2010, 4:55pm
3
zumm I dont care about create… I care about lang The routing works I get the $_GET var with /en/route just the problem is that CHtml does not recognize this route.
mikl
(Mike)
July 19, 2010, 6:00pm
4
But you said you have a problem with created URLs, that lang parameter appears at the end. That’s because when you create a URL like you posted in your example:
Html:link('test', array('create', 'lang' => 'th'));
There is no rule that matches ‘create’ in your setup.
mech7
(Chris De Kok)
July 20, 2010, 3:56am
5
As to my understanding it should apply to the following rule…
‘<lang:(th|en|jp)>/<_c>/<_a>/*’ => ‘<_c>/<_a>/’,
Or am I missing something am far from an expert about url rewrting? Cause it does work for getting the $_GET var… (and setting the language) I am using this with the language component…
http://www.yiiframework.com/extension/langhandler/
mikl
(Mike)
July 20, 2010, 8:26am
6
I think yii needs a full route when you create URLs to identify this rule. Can you try again and specify both, action + controller, like:
Html:link('test', array('user/create', 'lang' => 'th'));
mdomba
(Maurizio Domba Cerin)
July 20, 2010, 8:47am
7
Add one more rule:
'<lang:(th|en|jp)>/<_c>/<_a>' => '<_c>/<_a>/',
(without the ‘/*’ ) and it should work…
mech7
(Chris De Kok)
July 21, 2010, 2:45am
8
Thx but it doesnt seem to work
mikl
(Mike)
July 21, 2010, 7:31am
9
… so you don’t want to try creating a URL with a full route?
mech7
(Chris De Kok)
July 21, 2010, 11:20am
10
I want it to put the lang var in front not at the end… now the only way I can do it is…
<a href="<?php echo $this->createUrl(‘en/books/create’);?>"
mikl
(Mike)
July 21, 2010, 4:37pm
11
Ok, one more time. Why don’t you want to try my suggestion from above and create a URL with the full route ? Yii will not find the URL route you configured, if you don’t use the right route format ‘controller/action ’.
CHtml:link('test', array('book/create', 'lang' => 'th'));
mech7
(Chris De Kok)
July 22, 2010, 3:13am
12
Ok, one more time. Why don’t you want to try my suggestion from above and create a URL with the full route ? Yii will not find the URL route you configured, if you don’t use the right route format ‘controller/action ’.
CHtml:link('test', array('book/create', 'lang' => 'th'));
Ah I was not sure what you meant with that anyways it has the same result… creates a url like: /book/create/lang/th
not…
/th/book/create/
mikl
(Mike)
July 22, 2010, 7:35am
13
Ah, found it. It’s the last “/” in this line.
Please change
'<lang:(th|en|jp)>/<_c>/<_a>/*' => '<_c>/<_a>/',
to
'<lang:(th|en|jp)>/<_c>/<_a>' => '<_c>/<_a>',
There should not be any trailing or preceding slashes in your rules. You should also remove the * from all rules, too. It doesn’t make much sense here. The way you used it it above (/*) it says “here can be zero or any number of slashes”.
mech7
(Chris De Kok)
July 22, 2010, 7:38am
14
Thx but i already did that too as advised by mdomba still not working though
'urlManager'=>array(
'showScriptName' => false,
'urlFormat' => 'path',
'rules' => array(
'book/<id:\d+>/*'=>'books/view',
'<lang:(th|en|jp)>/book/<id:\d+>/*'=>'books/view',
'<lang:(th|en|jp)>/<_c>/<_a>' => '<_c>/<_a>/',
'<lang:a-z{2}_a-z{2,}>/<_m>/<_c>/<_a>' => '<_m>/<_c>/<_a>',
'gii'=>'gii',
'gii/<controller:\w+>'=>'gii/<controller>',
'gii/<controller:\w+>/<action:\w+>'=>'gii/<controller>/<action>',
),
),
mdomba
(Maurizio Domba Cerin)
July 22, 2010, 7:39am
15
You have to remove the last "/" in my example
change
'<lang:(th|en|jp)>/<_c>/<_a>' => '<_c>/<_a>/',
to
'<lang:(th|en|jp)>/<_c>/<_a>' => '<_c>/<_a>',
remove the last "/"
mikl
(Mike)
July 22, 2010, 7:41am
16
mdomba is right. You should copy exactly - and read what i said about trailing slashes . I tried your (fixed) rules, they work fine.
mech7
(Chris De Kok)
July 23, 2010, 2:49am
17
Ah ok thx it works… but causes another problem when adding more vars
‘<lang:(th|en|jp)>/<_c>/<_a>’ => ‘<_c>/<_a>’,
This will work…
CHtml::link('create', array('books/create', 'lang' => 'th'));
But this…
CHtml::link('books/create', array('books/create', 'lang' => 'th', 'id' => '1', 'test' => 'ok'));
Make the url look like:
/th/user/books/create?id=1&test=ok
The las parameters are not as path
mikl
(Mike)
July 23, 2010, 6:49am
18
Ooops, i have to take back one of my statements from above. The /* you had at the end of the rule does make sense. From the guide:
If the GET parameters passed to createUrl are more than those required by a rule, the additional parameters will appear in the query string. For example, if we call $this->createUrl(‘post/read’,array(‘id’=>100,‘year’=>2008)), we would obtain /index.php/post/100?year=2008. In order to make these additional parameters appear in the path info part, we should append /* to the rule. Therefore, with the rule post/<id:\d+>/*, we can obtain the URL as /index.php/post/100/year/2008.
So try this:
'<lang:(th|en|jp)>/<_c>/<_a>/*' => '<_c>/<_a>',
Sorry for the wrong advice
mech7
(Chris De Kok)
July 23, 2010, 10:45am
19
Ah great this is working… thx for the help was struggling too long with this one.