The documentation for Symfony 2.7 has been updated to use a compiler pass: https://symfony.com/doc/2.7/security/target_path.html
I imagine that this change will flow through to the later versions of the documentation soon.
// src/AppBundle/Security/Firewall/ExceptionListener.php
namespace AppBundle\Security\Firewall;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Firewall\ExceptionListener as BaseExceptionListener;
class ExceptionListener extends BaseExceptionListener
{
protected function setTargetPath(Request $request)
{
// Do not save target path for XHR requests
// You can add any more logic here you want
// Note that non-GET requests are already ignored
if ($request->isXmlHttpRequest()) {
return;
}
parent::setTargetPath($request);
}
}
// 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);
}
}
// src/AppBundle/AppBundle.php
namespace AppBundle;
use AppBundle\DependencyInjection\Compiler\ExceptionListenerPass;
class AppBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new ExceptionListenerPass());
}
}