symfony - cannot binding a kernel.view EventListener to FOSUserBundle -
i separated mobile , web requests of kernel.view
event listener.
the logic works this:
- if request coming mobile, load
xxx.mobile.twig
- if request coming web, load
xxx.html.twig
this working custombundle without problem. in addition i'm using fosuserbundle , hwioauthbundle of routes. checked var/logs/dev.log
, can't see kernel.view
events regarding these bundles routes , listener cannot work these bundles.
could give me idea how bind kernel.view
event bundles?
/** * @param getresponseforcontrollerresultevent $event * @return bool */ public function onkernelview(getresponseforcontrollerresultevent $event) { if (!$this->ismobilerequest($event->getrequest()->headers->get('user-agent'))) { return false; } $template = $event->getrequest()->attributes->get('_template'); if (!$template) { return false; } $templatereference = $this->templatenameparser->parse($template); if ($templatereference->get('format') == 'html' && $templatereference->get('bundle') == 'custombundle') { $mobiletemplate = sprintf( '%s:%s:%s.mobile.twig', $templatereference->get('bundle'), $templatereference->get('controller'), $templatereference->get('name') ); if ($this->templating->exists($mobiletemplate)) { $templatereference->set('format', 'mobile'); $event->getrequest()->attributes->set('_template', $templatereference); } } }
there few things should consider when debugging event related issues on symfony2.
events propagation can stopped
- it listener listening same event , stopping event propagation calling
$event->stoppropagation()
. in case make sure listener executed first (see point 2 below).
- it listener listening same event , stopping event propagation calling
event listeners have priorities
- when defining listener can set priority shown below:
view_response_listener: class: appbundle\eventlistener\viewresponselistener tags: # highest priority, earlier listener executed # @see http://symfony.com/doc/2.7/cookbook/event_dispatcher/event_listener.html#creating-an-event-listener - { name: kernel.event_listener, event: kernel.view, method: onkernelview, priority: 101 }
the other optional tag attribute called priority, defaults 0 , controls order in listeners executed (the highest priority, earlier listener executed). useful when need guarantee 1 listener executed before another. priorities of internal symfony listeners range -255 255 own listeners can use positive or negative integer.
source: http://symfony.com/doc/2.7/cookbook/event_dispatcher/event_listener.html#creating-an-event-listener
usually dispatch of events done in bootstrap file
- make sure regenerate bootstrap file (and clear cache you're @ it!), if you're playing priorities and/or configuration.
composer run-script post-update-cmd php app/console cache:clear --env=dev
the composer
post-update-cmd
regenerate bootstrap file other things reinstalling assets don't need. regenerate bootstrap file check answer here.
Comments
Post a Comment