Reputation: 1177
I am struggling with an issue with configuration between Symfony2 bundles.
Basically, I have a 'classic' structure.
app/config/*.yml holds various global configurations and per environment routes
/src/Item1/Bundle1 to Bundle10 are some bundles that define a core application
/src/Api/ApiBundle is a bundle that defines an "api" (a set of web services that I like to think of as REST)
The issue:
Bundle1 defines an ExceptionListener for kernel.exception that is shared between Bundle1 to 10.
In ApiBundle, I need to define a different Listener for the same kernel.exception so that for routes handled by the ApiBundle it will trigger in exception cases.
How can I have this? So far it seems that the last bundle loaded in AppKernel that defines the Listener overrides the listener and it triggers in all cases regardless of routes or bundles.
Is there a 'symfony' way to do it? To me it seems that the bundles should be fairly independent.
Thanks!
Upvotes: 1
Views: 44
Reputation: 2123
I don't know if there is a way to achieve what you want, but alternatively you could have one listener and handle the event depending on which bundle it comes from:
namespace Your\MainBundle\EventListener;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
class YourExceptionListener
{
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
$namespace = new \ReflectionObject( $event->getController() )->getNamespaceName();
switch ( $namespace )
{
case 'Acme\\DemoBundle':
// do whatever with $exception here
break;
case 'Item1\\Bundle1':
// do whatever with $exception here
break;
case 'Api\\ApiBundle':
// do whatever with $exception here
break;
default;
// default
}
}
}
Credits: https://stackoverflow.com/a/11125009/1591238
Upvotes: 1