symfony 1.4:如何将异常消息传递给error.html.php?

Hon*_*ong 5 symfony1 exception symfony-1.4

我尝试使用$message这里描述的特殊变量http://www.symfony-project.org/cookbook/1_2/en/error_templates但似乎这个变量没有在symfony 1.4中定义,至少它不包含传递给异常的消息这条路throw new sfException('some message')

你知道将此消息传递给error.html.php的其他方法吗?

Ger*_*rry 8

您需要进行一些自定义错误处理.我们自己实施了一个自定义symfony动作.但要小心,这个动作本身也可能引发异常,你需要考虑到这一点.

以下可能是一个好的开始.首先为事件添加一个监听器,一个好的地方是ProjectConfiguration.class.php:

$this->dispatcher->connect('application.throw_exception', array('MyClass', 'handleException'));
Run Code Online (Sandbox Code Playgroud)

使用事件处理程序可能足以满足您对异常的要求,例如,如果您只想将堆栈跟踪邮寄给管理员.我们希望转发自定义操作以显示和处理反馈表单.我们的事件处理程序看起来像这样:

class MyClass {
  public static function handleException(sfEvent $event) {
    $moduleName = sfConfig::get('sf_error_500_module', 'error');
    $actionName = sfConfig::get('sf_error_500_action', 'error500');
    sfContext::getInstance()->getRequest()->addRequestParameters(array('exception' => $event->getSubject()));
    $event->setReturnValue(true);
    sfContext::getInstance()->getController()->forward($moduleName, $actionName);
  }
}
Run Code Online (Sandbox Code Playgroud)

您现在可以配置模块和操作以转发到settings.yml中的异常

all:
  .actions:
    error_500_module:       error
    error_500_action:       error500
Run Code Online (Sandbox Code Playgroud)

在动作本身中,你现在可以做任何你想要的例外,例如.显示反馈表单以联系管理员.您可以使用$ request-> getParameter('exception')来获取异常