Yii框架:错误创建网址 [英] Yii framework: wrong creating urls
问题描述
我正在学习Yii,并遇到了网址创建问题.我有一个组件urlManager,如下所示:
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',
),
当我使用此功能时:
$this->createUrl('admin/useredit',array('id'=>'2'))
它正在创建'xxx.com/admin/useredit/.html2',但我希望使用'xxx.com/admin/useredit/2.html'
It's creating 'xxx.com/admin/useredit/.html2' but I expecting 'xxx.com/admin/useredit/2.html'
怎么了?
问题仅在CGridView中:
The problem is only in CGridView:
...
'value' => 'CHtml::link("$data->username","'.$this->createUrl('admin/useredit',array('id'=>$data->id)).'".CHtml::encode($data->id))',
...
推荐答案
您需要重新排列规则.将使用与您的路线匹配的第一条规则.因此,在您的情况下,这是<controller>/<action>
.您应该将admin/useredit
规则移到顶部.
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.
如果要在CGridView
列中创建URL,则需要用'
括住表达式.您也不需要调用createUrl()
,因为CHtml::link
会接受带有参数的路由.
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))'
这篇关于Yii框架:错误创建网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!