CHtml::link and url route..

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?

You have no rule for the route ‘create’.

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.

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.

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/

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'));

Add one more rule:




'<lang:(th|en|jp)>/<_c>/<_a>' => '<_c>/<_a>/',



(without the ‘/*’ ) and it should work…

Thx but it doesnt seem to work

… so you don’t want to try creating a URL with a full route? :)

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’);?>"

Ok, one more time. :rolleyes: 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/

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”.

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>',

            ),

        ),

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 "/"

mdomba is right. You should copy exactly - and read what i said about trailing slashes ;). I tried your (fixed) rules, they work fine.

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 :(

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:

So try this:


'<lang:(th|en|jp)>/<_c>/<_a>/*' => '<_c>/<_a>',

Sorry for the wrong advice ;)

:lol: Ah great this is working… thx for the help was struggling too long with this one.