Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2 - Manipulate request/response from the Kernel Exception Listener
    primarykey
    data
    text
    <p>I am building an administration panel for a website and I would like to change the view called when a 404 exception occurs <strong>but only</strong> for the admin application. <code>(path: /admin/*)</code></p> <p>I have already overdid the <code>error404.html.twig</code> view (at <code>app/Resources/TwigBundle/views/Exception/</code>) for the website.</p> <p>I thought of the kernel.exception event listener but now I am stuck with two things:</p> <ul> <li><p>Loading another error view only when the route starts with the prefix: <code>/admin/</code> </p> <pre><code>$route = $event-&gt;getRequest-&gt;get('_route')-&gt;render() //returns NULL </code></pre></li> <li><p>Calling the <code>$event-&gt;container-&gt;get('templating')-&gt;render()</code> function.</p></li> </ul> <p>I end up with an infinite loop (blank page) as the script fails.</p> <p>The only things I've got working are:</p> <ul> <li><p>Retrieving the exception code:</p> <pre><code>$exception = $event-&gt;getException(); $code = $exception-&gt;getCode(); </code></pre></li> <li><p>Creating a new response:</p> <pre><code>$response = new Response(); $event-&gt;setResponse($response); </code></pre></li> </ul> <p>Any suggestions on how to achieve this?</p> <h1>[EDIT]</h1> <p>The class:</p> <pre><code>namespace Cmt\AdminBundle\EventListener; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Bundle\TwigBundle\TwigEngine; class AdminActionListener { /** * @var ContainerInterface */ protected $container; /** * @var TwigEngine */ protected $templating; /** * @param ContainerInterface $container */ public function __construct(ContainerInterface $container, TwigEngine $templating){ // assign value(s) $this-&gt;container = $container; $this-&gt;templating = $templating; } /** * * @param GetResponseForExceptionEvent $event */ public function onKernelException(GetResponseForExceptionEvent $event) { // get exception $exception = $event-&gt;getException(); // get path $path = $event-&gt;getRequest()-&gt;getPathInfo(); /* * Redirect response to new 404 error view only * on path prefix /admin/ */ } } </code></pre> <p>And the services.yml:</p> <pre><code>services: cmt_admin.exception.action_listener: class: Cmt\AdminBundle\EventListener\AdminActionListener arguments: [@service_container] [@templating] tags: - { name: kernel.event_listener, event: kernel.exception, method: onKernelException } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload