加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 覆盖Symfony 2异常?

发布时间:2020-12-13 16:40:22 所属栏目:PHP教程 来源:网络整理
导读:根据此文档页面: http://symfony.com/doc/current/cookbook/controller/error_pages.html Symfony使用TwigBundle来控制异常的显示.但是,我不打算自定义显示,如文档中所述,我想覆盖它.我正在研究一个小的REST API,我想覆盖对我的bundle调用TwigBundle,进行自
根据此文档页面:

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

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

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

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

在异常监听器类上

//namespace 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: FQCNOfYourExceptionListener
  tags:
    - { name: kernel.event_listener,event: kernel.exception,method: onKernelException }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读