diff --git a/security/target_path.rst b/security/target_path.rst index f2993e91f4e..e78932aada4 100644 --- a/security/target_path.rst +++ b/security/target_path.rst @@ -15,38 +15,7 @@ URI was an XMLHttpRequest which returned a non-HTML or partial HTML response, the user is redirected back to a page which the browser cannot render. To get around this behavior, you would simply need to extend the ``ExceptionListener`` -class and override the default method named ``setTargetPath()``. - -First, override the ``security.exception_listener.class`` parameter in your -configuration file. This can be done from your main configuration file (in -``app/config``) or from a configuration file being imported from a bundle: - -.. configuration-block:: - - .. code-block:: yaml - - # app/config/services.yml - parameters: - # ... - security.exception_listener.class: AppBundle\Security\Firewall\ExceptionListener - - .. code-block:: xml - - - - - AppBundle\Security\Firewall\ExceptionListener - - - .. code-block:: php - - // app/config/services.php - use AppBundle\Security\Firewall\ExceptionListener; - - // ... - $container->setParameter('security.exception_listener.class', ExceptionListener::class); - -Next, create your own ``ExceptionListener``:: +class and override the default method named ``setTargetPath()``:: // src/AppBundle/Security/Firewall/ExceptionListener.php namespace AppBundle\Security\Firewall; @@ -69,4 +38,42 @@ Next, create your own ``ExceptionListener``:: } } -Add as much or as little logic here as required for your scenario! +By preventing ``setTargetPath()`` from being called on the parent, the Security component +won't retain the request URI. Add as much or as little logic here as required for your scenario! + +Next, create the ``ExceptionListenerPass`` to replace the definition of the default +``ExceptionListener`` with the one you just created. Make sure to use the name of +the firewall in your security configuration:: + + // src/AppBundle/DependencyInjection/Compiler/ExceptionListenerPass.php + namespace AppBundle\DependencyInjection\Compiler; + + use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; + use Symfony\Component\DependencyInjection\ContainerBuilder; + use AppBundle\Security\Firewall\ExceptionListener; + + class ExceptionListenerPass implements CompilerPassInterface + { + public function process(ContainerBuilder $container) + { + // Use the name of your firewall for the suffix e.g. 'secured_area' + $definition = $container->getDefinition('security.exception_listener.secured_area'); + $definition->setClass(ExceptionListener::class); + } + } + +Finally, add a compiler pass to tie it all together:: + + // src/AppBundle/AppBundle.php + namespace AppBundle; + + use AppBundle\DependencyInjection\Compiler\ExceptionListenerPass; + + class AppBundle extends Bundle + { + public function build(ContainerBuilder $container) + { + $container->addCompilerPass(new ExceptionListenerPass()); + } + } +