为什么 Yii 模块找不到系统视图? [英] Why Yii module does not find system views?

查看:23
本文介绍了为什么 Yii 模块找不到系统视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在应用主配置中:

'errorHandler'=>array(//使用站点/错误"操作来显示错误'errorAction'='站点/默认/错误',),

在/protected/modules/site/controllers/我有 DefaultController.php 有动作错误:

公共函数 actionError(){if($error=Yii::app()->errorHandler->error){if(Yii::app()->request->isAjaxRequest)echo $error['message'];别的$this->render('error', $error);}}

但是如果我有错误,我会看到:<代码><预>DefaultController 找不到请求的视图错误".(/home/web/framework/web/CController.php:897)

0/home/web/framework/web/CController.php(800): CController->renderPartial('error', Array, true)1/home/web/apps/myapp/protected/modules/site/controllers/SiteController.php(67): CController->render('error', Array)2/home/web/framework/web/actions/CInlineAction.php(50): SiteController->actionError()3/home/web/framework/web/CController.php(309): CInlineAction->runWithParams(Array)4/home/web/framework/web/CController.php(287): CController->runAction(Object(CInlineAction))5/home/web/framework/web/CController.php(266): CController->runActionWithFilters(Object(CInlineAction), Array)6/home/web/framework/web/CWebApplication.php(283): CController->run('error')7/home/web/framework/base/CErrorHandler.php(332): CWebApplication->runController('site/site/error')8/home/web/framework/base/CErrorHandler.php(205): CErrorHandler->render('error', Array)9/home/web/framework/base/CErrorHandler.php(130): CErrorHandler->handleException(Object(CHttpException))10/home/web/framework/base/CApplication.php(713): CErrorHandler->handle(Object(CExceptionEvent))11【内部函数】:CApplication->handleException(Object(CHttpException))

在 Yii 文档中:CErrorHandler 按照以下顺序查找视图对应的视图文件:

1.WebRoot/themes/ThemeName/views/system:这是当前活动主题下的系统视图目录.

2.WebRoot/protected/views/system:这是应用程序的默认系统视图目录.

3.yii/framework/views:这是Yii框架提供的标准系统视图目录.

为什么 Yii 在使用模块时在 yii/framework/views 中找不到视图?

解决方案

错误处理程序将错误作为参数 action 而不是错误 view.

错误信息也很明确,DefaultController 找不到视图.

使用模块时,将视图放在模块目录中,而不是在主 yii 视图文件夹中.在您的情况下,错误视图应该在 /protected/modules/site/views/default/error.php 中,更一般地说:/protected/modules/<moduleId>/views/<controllerId>/.php

要访问根视图,请使用 //.来自 关于视图名称解析的文档

<块引用>

根据名称查找视图文件.视图名称可以是以下格式之一:

  • 模块中的绝对视图:视图名称以单斜杠/"开头.在这种情况下,将在当前活动模块的视图路径下搜索视图.如果没有活动模块,将在应用程序的视图路径下搜索视图.
  • 应用程序中的绝对视图:视图名称以双斜杠//"开头.在这种情况下,将在应用程序的视图路径下搜索视图.此语法从 1.1.3 版开始可用.
  • 别名视图:视图名称包含点并引用路径别名.视图文件是通过调用 YiiBase::getPathOfAlias() 确定的.请注意别名视图不能被主题化,因为它们可以引用位于任意位置的视图文件.
  • 相对观点:否则.将在当前活动下搜索相对视图控制器的视图路径.

对于绝对视图和相对视图,对应的视图文件是一个PHP文件其名称与视图名称相同.该文件位于指定目录下.此方法将调用 CApplication::findLocalizedFile 来搜索本地化文件(如果有).

注意:这仅适用于控制器视图.

In app main config:

'errorHandler'=>array(
    // use 'site/error' action to display errors
    'errorAction'=>'site/default/error',
),

In /protected/modules/site/controllers/ I have DefaultController.php with action error:

public function actionError()
{
    if($error=Yii::app()->errorHandler->error)
    {
        if(Yii::app()->request->isAjaxRequest)
            echo $error['message'];
        else
            $this->render('error', $error);
    }
}

But if I have error, I see it:

DefaultController cannot find the requested view "error". (/home/web/framework/web/CController.php:897)

0 /home/web/framework/web/CController.php(800): CController->renderPartial('error', Array, true) 1 /home/web/apps/myapp/protected/modules/site/controllers/SiteController.php(67): CController->render('error', Array) 2 /home/web/framework/web/actions/CInlineAction.php(50): SiteController->actionError() 3 /home/web/framework/web/CController.php(309): CInlineAction->runWithParams(Array) 4 /home/web/framework/web/CController.php(287): CController->runAction(Object(CInlineAction)) 5 /home/web/framework/web/CController.php(266): CController->runActionWithFilters(Object(CInlineAction), Array) 6 /home/web/framework/web/CWebApplication.php(283): CController->run('error') 7 /home/web/framework/base/CErrorHandler.php(332): CWebApplication->runController('site/site/error') 8 /home/web/framework/base/CErrorHandler.php(205): CErrorHandler->render('error', Array) 9 /home/web/framework/base/CErrorHandler.php(130): CErrorHandler->handleException(Object(CHttpException)) 10 /home/web/framework/base/CApplication.php(713): CErrorHandler->handle(Object(CExceptionEvent)) 11 [internal function]: CApplication->handleException(Object(CHttpException))

In Yii documentation: CErrorHandler searches for the view file corresponding to a view in the following order:

1.WebRoot/themes/ThemeName/views/system: this is the system view directory under the currently active theme.

2.WebRoot/protected/views/system: this is the default system view directory for an application.

3.yii/framework/views: this is the standard system view directory provided by the Yii framework.

Why Yii not find views in yii/framework/views when using modules ?

解决方案

Error handler takes as param error action not error view.

Also error message is clear, that DefaultController cannot find view.

When using modules, place views in module directory, not in main yii view folder. In your case error view should be in /protected/modules/site/views/default/error.php, more generally: /protected/modules/<moduleId>/views/<controllerId>/<viewName>.php

To access root view, use //. From docs about view name resolving

Finds a view file based on its name. The view name can be in one of the following formats:

  • absolute view within a module: the view name starts with a single slash '/'. In this case, the view will be searched for under the currently active module's view path. If there is no active module, the view will be searched for under the application's view path.
  • absolute view within the application: the view name starts with double slashes '//'. In this case, the view will be searched for under the application's view path. This syntax has been available since version 1.1.3.
  • aliased view: the view name contains dots and refers to a path alias. The view file is determined by calling YiiBase::getPathOfAlias(). Note that aliased views cannot be themed because they can refer to a view file located at arbitrary places.
  • relative view: otherwise. Relative views will be searched for under the currently active controller's view path.

For absolute view and relative view, the corresponding view file is a PHP file whose name is the same as the view name. The file is located under a specified directory. This method will call CApplication::findLocalizedFile to search for a localized file, if any.

NOTE: This applies only to controller views.

这篇关于为什么 Yii 模块找不到系统视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆