PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 30, 2022

[FIXED] Yii, createUrl not working as expected

 January 30, 2022     yii, yii-routing     No comments   

Issue

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>',
               ),

Solution

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>


Answered By - Zerg Ling
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing