1

I want to override the setTargetPath from the default ExceptionListener (documentation). But I need additional service to handle this.

In my opinion there is only the way to override the service definition, copy it to my service definitions and create an own constructor, but I don't like this approach.

Is there any other way to do this?

CSchulz
  • 10,882
  • 11
  • 60
  • 114
  • What kind of targetPath do you want to override? The default ExceptionListener don't have any setTargetPath method – ghostika Oct 14 '14 at 12:28
  • This one https://github.com/symfony/Security/blob/master/Http/Firewall/ExceptionListener.php, that is the default one from the security component. I don't know why you say there is none containing target path. – CSchulz Oct 14 '14 at 12:38
  • Yes, now i see, but that exception listener is called up on authentication exception, not the base symfony default – ghostika Oct 14 '14 at 12:40
  • Uhm ... I don't understand what you mean. – CSchulz Oct 14 '14 at 12:42

2 Answers2

1

As for answer, if you are using form_login type, you can set it to a constant route where the redirect after login should happen. Config

You should set these 2 keys:

            always_use_default_target_path: true
            default_target_path:            /route_name_for_redirect

Or option B, you use a success handler service, where you simply returns a RedirectResponse

ghostika
  • 1,473
  • 1
  • 12
  • 23
1

I have found another approach using a compiler pass, following steps are necessary:

  1. Create your own class extending ExceptionListener and add for your dependencies a method
  2. Create a compiler pass containing something like the following:

    class WebCompilerPass implements CompilerPassInterface
    {
    
        public function process(ContainerBuilder $container)
        {
            $definition = $container->getDefinition('security.exception_listener');
            $definition->addMethodCall('yourmethod', [ new Reference('your dependency') ]);
        }
    }
    
  3. Register your compiler pass
  4. Overwrite the exception listener class parameter:

    security.exception_listener.class: AcmeBundle\EventListener\SecurityExceptionListener
    
CSchulz
  • 10,882
  • 11
  • 60
  • 114