Yii, createUrl not working as expected

10,815

Solution 1

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>

Solution 2

First of all , why are you even trying to access controller from the view? It's pointless and it goes against all the patterns.

Besides, you can create URLs with Yii::app()->createUrl(). Also, you might benefit from reading the documentation on how to utilize this function.

The value 'sources' does not fit any of the routing patterns .. try maybe following:

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

Solution 3

You forgot the echo:

    <a href="<?php 
      echo Yii::app()->createUrl("[/][CONTROLLER][/]ACTION", array("view"=>"VIEW")); 
    ?>">Link name</a>
Share:
10,815
rix
Author by

rix

Updated on June 04, 2022

Comments

  • rix
    rix almost 2 years

    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>',
                   ),
    
  • bool.dev
    bool.dev almost 12 years
    there's actually an implementation of createUrl in CController where you can just specify the action, in which case the current controller id is prepended. although i agree with the fact that there's no point in accessing the controller, when he can directly use $this to refer to the current controller.
  • tereško
    tereško almost 12 years
    @bool.dev While there exists such a method, it does not mean, that you should access it from view. Hell .. IMHO, it should be private. If view starts to directly manipulate controller instances, then one could as well just stop even pretending, that this is MVC. It's not. There exist no MVC-inspired design pattern which permit view instances to request information from controller. None.
  • tereško
    tereško almost 12 years
    Also ... is it just me, or this is pointless code duplication.
  • bool.dev
    bool.dev almost 12 years
    ok, fair enough, i see your point, from a strict MVC perspective. but in this scenario, where does Yii::app fit? its neither controller, nor view, nor model, right? And there's hardly any duplication, only addition of details at each level, i.e, finally all versions of createUrl call CUrlRule's version of createUrl, or createUrlDefault is called depending on some condition.
  • tereško
    tereško almost 12 years
    Had to do some thinking on the subject (my understanding of MVC is still quite primitive). It seems, that the URL generation should definitely be related to View, thus it would have routing related structures (ones, that are actually responsible for parsing URLs) as a dependency. And the Router could hold the current location as well. Ehh .... the whole confusion stems from the fact, that MVC design pattern was made for desktop application and transition to web environment is not as simple as some would think.
  • bool.dev
    bool.dev almost 12 years
    well mine is even more primitive, so thanks for reinforcing the view-url relation. there should just be a whole new pattern for web.
  • tereško
    tereško almost 12 years
    There are few: Model2 (which almost nobody uses), MVP (most web frameworks .. with limited success), MVVM (asp.net mvc framework, several javascript frameworks) and HMVC (Kohana framework on php side, few Java frameworks).