How to write a URL rule in Yii framework?

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

i got a link

forum/thread/2

in my urlManager rules 'thread/<id:\d+>' => 'forum/thread',

how to change the rule and method createUrl?

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

to get in url

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

I am sorry for my english, thanks a lot

2

There are 2 answers

0
Pitchinnate On BEST ANSWER

URL Manager rule should look like this:

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

Then in your controller you would have this:

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

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

and with this: createUrl('any-value/forum/thread', array('id'=>$value->thread_id)) So you should get forum/any-value/thread/2

that should work!

But If you are inside the module called forum then you would do like that:

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

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