现在我正在尝试捕获类似这样的异常事件:
Original Answer翻译成"最初的回答"
try {
echo 1 / 0;
} catch (\Exception $e){
$subs = new ExceptionSubscriber();
$this->dispatcher->addSubscriber($subs);
};
我定义了一个名为ExceptionSubscriber的类,代码如下:
class ExceptionSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
KernelEvents::EXCEPTION => [
['processException', 10],
['exception', -10],
],
];
}
public function exception(ExceptionEvent $event)
{
echo 'test321';
}
public function processException(ExceptionEvent $event)
{
echo 'test123';
}
}
And this is my services.yaml
App\EventSubscriber\ExceptionSubscriber:
tags:
- { name: kernel.event_subscriber, event: kernel.exception }
我知道我捕获的常规PHP异常不是内核异常事件之一,所以我需要创建自定义异常事件,对吗?
我正在使用EventSubscriber而不是Listener来分派事件,这种方式好吗?
我是否需要分派这些事件,或者它们会以某种神奇的方式传递到订阅者那里?