Yii framework: wrong creating urls

 ̄綄美尐妖づ 提交于 2019-12-25 04:08:07

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!