Question

I'm new to web development, and I've spent hours trying to understand Yii and how it handle's URLs. I've read scores of tutorials and still don't understand it.

I'm trying to set the URL for a button widget, for a menu widget, for a Controller Action, and for a basic HTML link. It appears that Yii handles all of this routing differently, and I can't get it to work.

Here is one example. I need to get from a page in the protected/views/site folder to a module in protected/modules. ALL of the following commands give me the CException error SiteController cannot find the requested view [...] .../yii/framework/web/CController.php(878) file when issuing the following command ...

$this->render(Yii::app()->createURL('module/action'));
$this->render(Yii::app()->createURL('/module/action'));
$this->render(Yii::app()->createAbsoluteURL('module/action'));
$this->render(Yii::app()->createAbsoluteURL('/module/action'));
$this->render('module');
$this->render('/module');
$this->render('//module')
$this->render('module/action');
$this->render('/module/action');

I thought I was getting this error because createURL() gives me /app/index.php/module/action. Instead, I want http://mysite/app/index.php/module/action. Thanks to @Jonnny, I was able to get the full URL with createAbsoluteURL().
The problem is that I am still getting the same error. If I type http://mysite/app/index.php/module/action, I get the module I want, but if I try to render the page through Yii, I get the CException error.

The frustrating thing is that I can move between controllers quite easily in other areas of Yii. For example, if I set parameters to a YiiBooster widget with something like ...

array('label' => 'Menu Option',
      'url' => array('/controller/action')

... I get routed to http://mysite/app/index.php/controller/action no matter what controller I started with. The URL pattern for a controller/action is the same URL pattern for a module/action, so why can't Yii take me to the module/action?

UPDATE: Until I can understand Yii better, I am using a solution found on http://www.highnd.com/articles/yii-framework/yii-url-links-and-redirection. I can now access a module using the command: $this->redirect(Yii::app()->createAbsoluteURL('module'));

Était-ce utile?

La solution

Render is used to display a certain view file. You do not need to add a createUrl() before it. Just pass the controller/action you want to render, if you are rendering from a controller often you can just pass the view file name. So

$this->render('view', array('model' => $model)); // here view would be the view file (view.php) that relates to the model instance. 

The Yii booster widget you are using is asking for a specific url that you can pass as a route. In short, I think you're using render incorrectly. It is used for displaying a view file with a layout.

createUrl() - Creates a relative URL for the specified action defined in this controller.

createAbsoluteUrl() - Creates an absolute URL for the specified action defined in this controller.

You need createAbsoulteUrl() if you want a full URL.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top