I recently had to implement a few handlers and listeners in a symfony2 project that lead to the following problems:
Exception Handler was not called
Solution was to give my ExceptionHandler a priority > 0 like this:
1 2 3 4 5 6 |
# Handler for our controller exceptions.
acme.demobundle.execption.listener:
class: Acme\DemoBundle\Handler\ExceptionHandler
arguments: [@logger, %kernel.environment%]
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException, priority: 100 } |
A listener got a request that has no route (_route) set
According to the Profiler, my listener was called BEFORE the RouterListener. The RouterListener is called for an “early” and a “normal” routing.
Solution was to give my listener a negative priority, so it will be called AFTER the RouterListener that sets the _route parameter.
1 2 3 4 5 |
acme.demobundle.listener.request:
class: Acme\demoBundle\Security\Firewall\ApiListener
arguments: [@api, %api.path%]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest , priority: -10} |
If you want/need to dive deeper into the dependency injection, check out this page that documents the default listeners and their priorities:
http://symfony.com/doc/2.0/reference/dic_tags.html