Using Parameterizing Routes in CHtml.lnk

Hello guys,

In Url management tutorial having a special topic, that describe how to use UrmManage.

I’d tried it within CHtml::link, but it seem not work!

In my code, I were created a CGridview and in girdview, I have a column that content a CHtml::link like this (Id is following format: ‘xx xx’, so I use str_replace to format it to xxxx):




           array(

                'header' => 'ID',

                'type' => 'raw',

                'name' => 'id',

                'value' => 'CHtml::link($data->id, Yii::app()->createUrl("lichchay/viewbienso", array("id"=>str_replace(" ", "", $data->id),"month"=>date("m"),"year"=>date("Y"))), array("style" => "text-decoration: none"))',

            ),



In browser, it displays in status bar like this:

http://host_name/Quanlytour/viewbienso?id=2309&month=01&year=2011

It is not user-friendly url :), why don’t it following my url format that was config before in config/main.php?

And in config/main.php:




'urlManager'=>array(

            'showScriptName' => false,

	    'urlFormat'=>'path',

	    'rules'=>array(

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

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

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

                '<_c:(lichchay)>/<_a:(viewbienso)>/<id:\d>/<month:\w>/<year:\w>' => '<_c>/<_a>',

			),

		),



In UrlManager, I added the last one.

Cheers

You need to change the order of your rules. The first matching rule "wins". Match in this case means: All parameters used in the rule are present in the given parameters (which is different from "All given parameters are present in the rule"!). So put your last rule on top. Rules should be ordered from most special to most generic.

Thanks, let me try it :lol:

Beside the order, you may have a problem with the regular expression, since you accept only one decimal (\d) or letter (\w):




'<_c:(lichchay)>/<_a:(viewbienso)>/<id:\d+>/<month:\d+>/<year:\d+>' => '<_c>/<_a>',