Skip to content

Remove use of deprecated security.exception_listener.class parameter #7898

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 40 additions & 33 deletions security/target_path.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

<!-- app/config/services.xml -->
<parameters>
<!-- ... -->
<parameter key="security.exception_listener.class">AppBundle\Security\Firewall\ExceptionListener</parameter>
</parameters>

.. 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;
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use the kernel instead as of 3.3?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HeahDude what do you mean?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean using https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpKernel/Kernel.php#L446 instead, but this is not the good place to ask, and I guess will be part of a bigger update to get rid of AppBundle in the docs. So never mind :)

{
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new ExceptionListenerPass());
}
}