如何在Yii框架中编写URL规则?

时间:2013-07-30 19:29:11

标签: yii yii-routing yii-url-manager

<?php echo CHtml::link($value->title, array(Yii::app()->createUrl('forum/thread', array('id'=>$value->thread_id)))); ?>

我有一个链接

  

论坛/线程/ 2

在我的urlManager规则'thread/<id:\d+>' => 'forum/thread',

如何更改规则和方法createUrl?

createUrl('any-value/forum/thread', array('id'=>$value->thread_id))

进入网址

  

forum / any-value / thread / 2或forum / php-for-newbies / thread / 2

对不起我的英语,非常感谢

2 个答案:

答案 0 :(得分:0)

试试这个: 'forum/any-value/thread/<id:\d+>' => 'any-value/forum/thread',

并且用这个: createUrl('any-value/forum/thread', array('id'=>$value->thread_id)) 所以你应该得到forum/any-value/thread/2

应该有用!

但是如果你在名为forum的模块中,那么你会这样做:

'any-value/thread/<id:\d+>' => 'any-value/forum/thread',

并且用这个: createUrl('any-value/forum/thread', array('id'=>$value->thread_id))

答案 1 :(得分:0)

网址管理器规则应如下所示:

'forum/<title:\w+>/thread/<id:\d+>' => 'forum/thread', //make sure this is listed first so it has priority
'thread/<id:\d+>' => 'forum/thread',

然后在你的控制器中你会得到这个:

public function actionThread($id,$title=null) {
    //$title will contain title from url if sent
}