问题
I'm learning Yii and got into url creating problem. I have a component urlManager as follows:
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'admin/useredit/<id:\d+>'=>'admin/useredit',
),
'showScriptName'=>false,
'urlSuffix'=>'.html',
),
When I use this:
$this->createUrl('admin/useredit',array('id'=>'2'))
It's creating 'xxx.com/admin/useredit/.html2' but I expecting 'xxx.com/admin/useredit/2.html'
What may be wrong?
Edit: The problem is only in CGridView:
...
'value' => 'CHtml::link("$data->username","'.$this->createUrl('admin/useredit',array('id'=>$data->id)).'".CHtml::encode($data->id))',
...
回答1:
You need to reorder your rules. The first rule that matches your route will be used. So in your case this is <controller>/<action>
. You should move your admin/useredit
rule to the top.
If you want to create an URL in a CGridView
column you need to surround your expression with '
. You also don't need to call createUrl()
because CHtml::link
will accept a route with parameters.
'value'=>'CHtml::link($data->username,array("admin/useredit","id"=>$data->id))'
来源:https://stackoverflow.com/questions/17669643/yii-framework-wrong-creating-urls