CHtml::link not generating URL in path format

Hello,

I have urlFormat set to ‘path’ in my main config.

I’m trying to generate a link in my view file. I’m using the following code:


CHtml::link($data->totalCount.' items', array('collection/browse', 'by' => 'tag', 'id' => $data->id))

I’m trying to build a link with the format:


http://mysite.com/collection/browse/by/tag/id/25

But the link that the code generates is:


http://mysite.com/collection/browse/25?by=tag

CHtml::link uses normalizeUrl(), which uses createUrl(). The special topic page (http://www.yiiframework.com/doc/guide/1.1/en/topics.url) says that createUrl will use the ‘path’ format if it is configured this way, but it doesn’t seem to be doing that.

Is this a bug, or am I doing something wrong. Should I just use hardcoded anchor link tags instead of CHtml::link?

Thanks.

hi,

this should be configurable in your config/main.php in the urlManager. Can you please paste your urlManager setup.

If you would just like to eliminate the query string and puts the GET parameters into the path info part of URL you should just define ‘urlFormat’=>‘path’, in your url manager and leave the urlRules empty.

Another approach would be to have the following url rule:


'<controller:\w+>/<action:\w+>/<by:\w+>/<id:\d+>'=>'<controller>/<action>',

, which will further remove the GET parameters from the url and will bind only their values.

Please note that <by:\w+> allowes the by tag to be populated with almost any character. If you would like to make it more strict you could use (tag|tag2|tag3…etc) if you have a certain set of options.

hope this helps

bettor

Thanks so much bettor. That worked perfectly!

I didn’t realise the rules also determined how URLs were generated, I thought they were just used to route the request.

Cheers,

crikey

This section of the guide can be very helpful:

http://www.yiiframework.com/doc/guide/1.1/en/topics.url