我在Zend应用程序中有许多模块。 每个模块都会有错误和例外,对吧? 所以我在Application模块中创建了错误页面,它可以工作。
但是,在其他模块中,它们无法使用错误页面。 那么我该如何重用错误页面呢?
这是Application模块中的module.config.php文件:
return array( ..., 'controllers' => array( 'invokables' => array( 'Application\Controller\Index' => 'Application\Controller\IndexController' ), ), 'view_manager' => array( 'display_not_found_reason' => true, 'display_exceptions' => true, 'doctype' => 'HTML5', 'not_found_template' => 'error/404', 'exception_template' => 'error/index', 'template_map' => array( 'layout/layout' => __DIR__ . '/../view/layout/layout.phtml', 'application/index/index' => __DIR__ . '/../view/application/index/index.phtml', 'error/404' => __DIR__ . '/../view/error/404.phtml', 'error/index' => __DIR__ . '/../view/error/index.phtml', ), 'template_path_stack' => array( __DIR__ . '/../view', ), ), );当我使用以下代码手动返回HTTP 404响应时,会出现此问题:
if ( $information == null ) { $response = $this->getResponse(); $response->setStatusCode(404); return $response; }但是,当我访问非法资源时,例如http://example.com/something-do-not-exist 。 我将到达404错误页面,但异常The requested URL could not be matched by routing.
I have many modules in an Zend application. There will be errors and exceptions in every module, right? So I create the error page in the Application module, and it works.
However, in other modules, they can't use the error page. So what should I do to reuse the error page?
Here's the module.config.php file in Application module:
return array( ..., 'controllers' => array( 'invokables' => array( 'Application\Controller\Index' => 'Application\Controller\IndexController' ), ), 'view_manager' => array( 'display_not_found_reason' => true, 'display_exceptions' => true, 'doctype' => 'HTML5', 'not_found_template' => 'error/404', 'exception_template' => 'error/index', 'template_map' => array( 'layout/layout' => __DIR__ . '/../view/layout/layout.phtml', 'application/index/index' => __DIR__ . '/../view/application/index/index.phtml', 'error/404' => __DIR__ . '/../view/error/404.phtml', 'error/index' => __DIR__ . '/../view/error/index.phtml', ), 'template_path_stack' => array( __DIR__ . '/../view', ), ), );The problem occurs when I return a HTTP 404 response manually with following code:
if ( $information == null ) { $response = $this->getResponse(); $response->setStatusCode(404); return $response; }However, when I access an illegal resources like http://example.com/something-do-not-exist. I will reach the 404 error page with the exception The requested URL could not be matched by routing.
最满意答案
您列出的配置在应用程序模块中定义 ,但不是特定于该模块。
这是因为在应用程序引导期间,每个module.config.php都合并到一个配置数组中(使用array_merge_recursive() ) 。 这是您可以使用$serviceManager->get('Config');
因此,配置对应用程序是全局的,并且所有错误(异常)都应使用相同的错误模板( application/view/error/index.phtml )。
但请记住,模块的加载顺序很重要; 因为最后加载的模块将始终覆盖现有配置。
The configuration you have listed is defined in the application module, however it is not specific to that module.
This is because during the application bootstrap each module.config.php is merged together into one configuration array (using array_merge_recursive()). This is the configuration you can get using $serviceManager->get('Config');
So the configuration is global to the application and all errors (exceptions) should use the same error template (application/view/error/index.phtml).
Keep in mind however that the loading order of the modules is important; as modules loading last will always overwrite existing configuration.
更多推荐
发布评论