Zend框架 - 错误页面为PHP致命错误 [英] Zend framework - error page for PHP fatal errors

查看:192
本文介绍了Zend框架 - 错误页面为PHP致命错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Zend 1框架的PHP webapp上,如果代码引发异常,我会收到一个很好的错误页面,我的品牌等。

On a PHP webapp using the Zend 1 framework, if the code throws an exception, I get a nice error page with my branding etc.

如果代码遇到一个PHP致命的错误(例如,当对象引用意外为null时,非对象的方法调用),然后我只收到一个裸露的Apache 500错误页面。

If the code encounters a PHP fatal error (e.g. "method call on non-object" when an object reference is unexpectedly null), then I just get a bare Apache 500 error page.

我可以在后一种情况下得到一个很好的错误页面?

How can I get a nice error page in the latter case?

我尝试过的东西:


  • 如果设置了PHP inidisplay_errors,那么我只收到致命的错误消息作为纯文本

  • 如果没有设置display_errors,那么我得到了Apache的默认500错误页面

  • 在这种情况下,ApacheErrorDocument指令似乎被忽略

推荐答案

如果这可以帮助任何人通过一点点黑客获得完整的Zend错误页面(应用程序布局),以显示致命错误。

In case this helps anyone I was able, via a bit of hackery, to get the full Zend error page (with application layout) to display in the case of fatal errors.

希望有一个更简单的方法做这个。

Hopefully there is a much easier way to do this. If so, please add an answer with your alternative.

将此添加到引导程序中:

Add this to the bootstrap:

  /**
   * Sets up a register_shutdown_function hook to give a nice error page when a
   * PHP fatal error is encountered.
   */
  protected function _initFatalErrorCatcher()
  {
      register_shutdown_function(array($this, 'onApplicationShutdown'));
  }

  public function onApplicationShutdown()
  {
      $error = error_get_last();
      $wasFatal = ($error && ($error['type'] === E_ERROR) || ($error['type'] === E_USER_ERROR));
      if ($wasFatal)
      {
          $frontController = Zend_Controller_Front::getInstance();
          $errorHandler = $frontController->getPlugin('Zend_Controller_Plugin_ErrorHandler');
          $request = $frontController->getRequest();
          $response = $frontController->getResponse();

          // Add the fatal exception to the response in a format that ErrorHandler will understand
          $response->setException(new Exception(
              "Fatal error: $error[message] at $error[file]:$error[line]",
              $error['type']));

          // Call ErrorHandler->_handleError which will forward to the Error controller
          $handleErrorMethod = new ReflectionMethod('Zend_Controller_Plugin_ErrorHandler', '_handleError');
          $handleErrorMethod->setAccessible(true);
          $handleErrorMethod->invoke($errorHandler, $request);

          // Discard any view output from before the fatal
          ob_end_clean();

          // Now display the error controller:
          $frontController->dispatch($request, $response);
      }
  }

您需要一个自定义路由器类来帮助: p>

You need a custom router class to help:

class My_Router_Rewrite extends Zend_Controller_Router_Rewrite
{
    public function route(Zend_Controller_Request_Abstract $request)
    {
        // If the ErrorHandler plugin has stashed the error in a request param, then
        // it will have already dealt with routing (see Bootstrap::onApplicationShutdown())
        // (Note that this param cannot be spoofed, since any user-supplied params
        // will be strings, not objects)
        if (is_object($request->getParam('error_handler'))) {
            return $request;
        } else {
            return parent::route($request);
        }
    }
}

(确保这个类在bootstrap中注册为您的路由器)

(make sure that this class is registered as your router in the bootstrap)

这篇关于Zend框架 - 错误页面为PHP致命错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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