根据此文档页面:
http://symfony.com/doc/current/cookbook/controller/error_pages.html
Symfony使用TwigBundle来控制异常的显示.但是,我不打算自定义显示,如文档中所述,我想覆盖它.我正在研究一个小的REST API,我想覆盖对我的bundle调用TwigBundle,进行自己的异常处理(就REST而言:映射正确的HTTP状态代码和纯文本正文响应).
我找不到任何关于这个的内容,手册上的参考文献并不是那么好,特别是在内核部分.也许有人已经这样做了,可以帮助我吗?谢谢.
Mun*_*Das 13
您应该创建一个侦听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.
}
}
}
Run Code Online (Sandbox Code Playgroud)
服务声明将是
//services.yml
kernel.listener.yourlisener:
class: FQCN\Of\YourExceptionListener
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8660 次 |
| 最近记录: |