1

I'm using the CHtml::link() function, and I have a problem with the index action.

The code in view:

echo CHtml::link('Watch it', array('video/index', 'id' => $id));

The urlManager part in config:

'urlManager' => array(
        'urlFormat' => 'path',
        'rules' => array(
            '<id>/<action>' => 'video/<action>',
            '<id>' => 'video/index'
        )
    )

What I want it to do is to create a link to:

http://localhost/123

Instead, it creates a link to:

http://localhost/123/index

How can I remove the '/index' part?

Creating a function that will handle this (replace '/index' with nothing) will be the best solution for this?

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

3

You can use Yii::app()->createUrl in chtml link function

echo CHtml::link('Watch it', Yii::app()->createUrl('video/index', 'id' => $id));

UPD Try this

'urlManager' => array(
        'urlFormat' => 'path',
        'rules' => array( 
            '<id>' => 'video/index'
            '<id>/<action>' => 'video/<action>',

        )
    )
styopdev
  • 2,604
  • 4
  • 34
  • 55
  • Unfortunately, it creates the same link. – user3803707 Aug 03 '14 at 12:19
  • @user3803707 I just tested and this solution worked for me. Rather, just the rules change was sufficient, the `createUrl` method is unnecessary. Did you notice styopdev swapped the order of the rules? – Willem Renzema Aug 04 '14 at 07:42