我在Symfony中使用调度事件,我不明白,因为我有两次kernel.response
。
控制器:
/**
* @Route("/evento", name="evento")
*/
public function eventoAction()
{
$dispatcher = new EventDispatcher();
$subscriber = new StoreSubriber();
$dispatcher->addSubscriber($subscriber);
$event = new FilterOrderEvent($order);
$dispatcher->dispatch(StoreEvents::STORE_ORDER,$event);
return $this->render('::event.html.twig');
}
订户:
namespace AppBundle\Events;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
class StoreSubriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array(
'kernel.request' => array(
array('onKernelResponsePre', -10),
array('onKernelResponseMid', 5),
array('onKernelResponsePost', 0),
),
'store.order' => array('onStoreOrder', 0),
);
}
public function onKernelResponsePre(GetResponseEvent $event)
{
echo "<br>";
echo "onKernelResponsePre";
}
public function onKernelResponseMid(GetResponseEvent $event)
{
echo "<br>";
echo "onKernelResponseMid";
}
public function onKernelResponsePost(GetResponseEvent $event)
{
echo "<br>";
echo "onKernelResponsePost";
}
public function onStoreOrder(FilterOrderEvent $event)
{
echo "<br>";
echo "ORDER";
}
}
service.yml:
services:
user_check_token:
class: AppBundle\Events\StoreSubriber
tags:
- { name: kernel.event_subscriber }
我的输出:
onKernelResponseMid
onKernelResponsePost
onKernelResponsePre
ORDER
Events ( twig )
onKernelResponseMid
onKernelResponsePost
onKernelResponsePre
我的问题是,为什么我会看到onKernelResponseMid
,onKernelResponsePost
和onKernelResponsePre
两次?
答案 0 :(得分:0)
我会说,因为你订阅了两次这个活动。
eventoAction
$dispatcher->addSubscriber($subscriber);
tags: - { name: kernel.event_subscriber }
尝试从eventoAction中删除此$dispatcher->addSubscriber($subscriber);
。
其他可能的原因
{{ render(controller('AcmeArticleBundle:Article:recentArticles') }}
。由于这是另一个真实的请求,事件将触发。要处理此问题,您需要向订阅者添加条件以仅过滤您要处理的请求如何调试子请求
要轻松找出是否有任何子请求在哪里,您可以查看Web调试工具栏。
这样您就可以找到是否有任何子请求及其来源。