Yii, createUrl not working as expected

rix picture rix · Jul 14, 2012 · Viewed 10.8k times · Source

I'm doing the following in a view:

 <li><a href='<? Yii::app()->controller->createUrl('sources'); ?>'>sources</a></li>

However 'sources' is not appended to the path, instead the code just returns the path to the current controller.

Could anyone suggest why this might me? The code is in a module.

My url rules are as follows:

'rules'=>array(
 '<controller:\w+>/<id:\d+>'=>'<controller>/view',
 '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
 '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
               ),

Answer

Zerg Ling picture Zerg Ling · Jul 15, 2012

You should try 2 things

To get an absolute URL and not relative to the current controller or action add a leading 'slash' like:

    Yii::app()->createUrl('/sources/view');

You should also make sure you are not doing something stupid like forgetting to use echo :) that happens to me sometimes...

    <li><a href='<? echo Yii::app()->controller->createUrl('sources'); ?>'>sources</a></li>