yiiyii-routingyii-url-manager

How to write a URL rule in Yii framework?


<?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


Solution

  • 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
    }