在Zend Framework 2中的不同模块中使用相同的错误处理程序(Use the same error handler in different modules in Zend Framewor

编程入门 行业动态 更新时间:2024-10-27 10:33:58
在Zend Framework 2中的不同模块中使用相同的错误处理程序(Use the same error handler in different modules in Zend Framework 2)

我在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.

更多推荐

本文发布于:2023-07-26 16:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1278173.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   错误   程序   Framework   Zend

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!