php – 覆盖Symfony 2异常?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 覆盖Symfony 2异常?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
根据此文档页面

http://symfony.com/doc/current/cookbook/controller/error_pages.html

Symfony使用TwigBundle来控制异常的显示.但是,我不打算@R_301_2483@显示,如文档中所述,我想覆盖它.我正在研究一个小的REST API,我想覆盖对我的bundle调用TwigBundle,进行自己的异常处理(就REST而言:映射正确的HTTP状态代码纯文本正文响应).

我找不到任何关于这个的内容,手册上的参考文献并不是那么好,特别是在内核部分.也许有人已经这样做了,可以帮助我吗?谢谢.

应该创建一个侦听kernel.exception事件的侦听器.在该侦听器的onKernelException方法中,您可以检查您的异常,例如

在异常监听器类上

//@R_360_1507@pace declarations
  class YourExceptionListener
  {

      public function onKernelException(GetResponseForExceptionEvent $event)
      {
        $exception =  $event->getException();
        if ($exception instanceof YourException) {
            //create response,set status code etc.
            $event->setResponse($response); //event will stop PRopagating here. Will not call other listeners.
        }
      }
  }

服务声明将是

//services.yML
 kernel.listener.yourlisener:
  class: FQCN\Of\YourExceptionListener
  tags:
    - { name: kernel.event_listener,event: kernel.exception,method: onKernelException }

脚本宝典总结

以上是脚本宝典为你收集整理的php – 覆盖Symfony 2异常?全部内容,希望文章能够帮你解决php – 覆盖Symfony 2异常?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。