2

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>',
               ),
tereško
  • 58,060
  • 25
  • 98
  • 150
rix
  • 10,104
  • 14
  • 65
  • 92

3 Answers3

12

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>
Zerg Ling
  • 233
  • 1
  • 3
3

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');.
tereško
  • 58,060
  • 25
  • 98
  • 150
  • there's actually an implementation of [createUrl in CController](http://www.yiiframework.com/doc/api/1.1/CController#createUrl-detail) 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. – bool.dev Jul 14 '12 at 17:24
  • @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 Jul 14 '12 at 17:32
  • Also ... is it just me, or this is pointless code duplication. – tereško Jul 14 '12 at 17:34
  • 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](http://www.yiiframework.com/doc/api/1.1/CUrlRule#createUrl-detail), or [createUrlDefault](http://www.yiiframework.com/doc/api/1.1/CUrlRule#createUrlDefault-detail) is called depending on some condition. – bool.dev Jul 14 '12 at 17:46
  • 1
    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. – tereško Jul 14 '12 at 19:54
  • well mine is even more primitive, so thanks for reinforcing the view-url relation. there should just be a whole new pattern for web. – bool.dev Jul 15 '12 at 01:13
  • 1
    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). – tereško Jul 15 '12 at 01:42
1

You forgot the echo:

    <a href="<?php 
      echo Yii::app()->createUrl("[/][CONTROLLER][/]ACTION", array("view"=>"VIEW")); 
    ?>">Link name</a>
rapttor
  • 396
  • 3
  • 7