Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 - Manipulate request/response from the Kernel Exception Listener

I am building an administration panel for a website and I would like to change the view called when a 404 exception occurs but only for the admin application. (path: /admin/*)

I have already overdid the error404.html.twig view (at app/Resources/TwigBundle/views/Exception/) for the website.

I thought of the kernel.exception event listener but now I am stuck with two things:

  • Loading another error view only when the route starts with the prefix: /admin/

    $route = $event->getRequest->get('_route')->render()
    //returns NULL
    
  • Calling the $event->container->get('templating')->render() function.

I end up with an infinite loop (blank page) as the script fails.

The only things I've got working are:

  • Retrieving the exception code:

    $exception = $event->getException();
    $code = $exception->getCode();
    
  • Creating a new response:

    $response = new Response();
    $event->setResponse($response);
    

Any suggestions on how to achieve this?

[EDIT]

The class:

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->container = $container;
        $this->templating = $templating;
    }

    /**
     * 
     * @param GetResponseForExceptionEvent $event
     */
    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        // get exception
        $exception = $event->getException();

        // get path
        $path = $event->getRequest()->getPathInfo();

        /*
        * Redirect response to new 404 error view only
        * on path prefix /admin/ 
            */
        }
}

And the services.yml:

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 }
like image 552
LBridge Avatar asked Sep 19 '11 01:09

LBridge


2 Answers

For some reason, this worked:

// get exception
$exception = $event->getException();

// get path
$path = $event->getRequest()->getPathInfo();

if ($exception->getStatusCode() == 404 && strpos($path, '/admin') === 0){

    $templating = $this->container->get('templating');

    $response = new Response($templating->render('CmtAdminBundle:Exception:error404.html.twig', array(
        'exception' => $exception
    )));

    $event->setResponse($response);
}

Which is basically what I was doing earlier with a different syntax...

@dmirkitanov Anyway, thanks for your help !

like image 121
LBridge Avatar answered Oct 20 '22 12:10

LBridge


You could try this one:

public function __construct(TwigEngine $templating)
{
    $this->templating = $templating;
}

public function onKernelException(GetResponseForExceptionEvent $event)
{
    static $handling;

    $exception = $event->getException();

    if (true === $handling) {
        return;
    }
    $handling = true;

    $code = $exception->getCode();

    if (0 !== strpos($event->getRequest()->getPathInfo(), '/admin') && 404 === $code) {
        $message = $this->templating->render('AcmeBundle:Default:error404new.html.twig', array());
        $response = new Response($message, $code);
        $event->setResponse($response);
    }

    $handling = false;
}

$templating variable can be passed in services.xml:

<service id="acme.exception.listener" class="%acme.exception.listener.class%">
    <tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" />
    <argument type="service" id="templating" />
</service>
like image 3
dmirkitanov Avatar answered Oct 20 '22 10:10

dmirkitanov