首页 文章

YUI createURL和urlManager之间的关系

提问于
浏览
0

我是Web开发的新手,我花了好几个小时试图理解Yii以及它如何处理URL . 我已经阅读了很多教程,但仍然不理解它 .

我让它运转起来 .

这是一个例子 . 发出以下命令时,以下命令的 I need to get from a page in the protected/views/site folder to a module in protected/modules. ALL 给出了CException错误 SiteController cannot find the requested view [...] .../yii/framework/web/CController.php(878) 文件...

$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');

我以为我收到了这个错误,因为 createURL() 给了我 /app/index.php/module/action . 相反,我想 http://mysite/app/index.php/module/action . 感谢@Jonnny,我能够获得 createAbsoluteURL() 的完整网址 .
问题是我仍然遇到同样的错误 . 如果我输入 http://mysite/app/index.php/module/action ,我会得到我想要的模块,但如果我尝试通过Yii渲染页面,我会收到CException错误 .

令人沮丧的是,我可以在Yii的其他区域轻松地在控制器之间移动 . 例如,如果我将参数设置为YiiBooster小部件,例如...

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

......无论我开始使用什么控制器,我都会被路由到 http://mysite/app/index.php/controller/action . 控制器/动作的URL模式与模块/动作的URL模式相同,为什么Yii不能带我进入模块/动作?

UPDATE: 直到我能更好地理解Yii,我正在使用http://www.highnd.com/articles/yii-framework/yii-url-links-and-redirection上找到的解决方案 . 我现在可以使用以下命令访问模块: $this->redirect(Yii::app()->createAbsoluteURL('module'));

1 回答

  • 4

    渲染用于显示某个视图文件 . 您不需要在它之前添加createUrl() . 只需传递要渲染的控制器/操作,如果从控制器渲染,通常只需传递视图文件名即可 . 所以

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

    您正在使用的Yii助推器小部件是要求您可以作为路线传递的特定网址 . 简而言之,我认为你正在使用渲染错误 . 它用于显示带有布局的视图文件 .

    createUrl() - 为此控制器中定义的指定操作创建相对URL . createAbsoluteUrl() - 为此控制器中定义的指定操作创建绝对URL .

    如果需要完整的URL,则需要createAbsoulteUrl() .

相关问题