3

I'm trying to work out the best way of handling custom error pages within Symfony2.This includes 500 and 404's etc.

I can create my own custom templates (error404.html.twig etc) and render these out fine, the issue is , the app requires a few variables be passed into the base template for the page to remain consistent. Using the built in exception handler results in required variables not being available.

I have successfully setup a custom Exception Event Listener, and registered it as a service:

namespace MyCo\MyBundle\Listener;

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Bundle\TwigBundle\TwigEngine;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;



class MyErrorExceptionListener
{

    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        // We get the exception object from the received event
        $exception = $event->getException();

        if($exception->getStatusCode() == 404)
        {

            //$engine = $this->container->get('templating');
            //$content = $engine->render('MyBundle:Default:error404.html.twig');    
            //return $response = new Response($content);

            /* Also Tried */
            //$templating = $this->container->get('templating');    
            //return $this->render('MyBundle:Default:index.html.twig');



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

            $event->setResponse($response);

        }


    }
}

This doesn't work , as :$container is not available , meaning I cannot render my custom page.

So two questions really , is this the correct way to handle custom error pages, or should I pass the response off to a controller? If so , whats the best way of doing that?

If this is correct , how can I make the templating engine available within my Listener ?

2 Answers 2

9

You should add into yours Listener

/**
 *
 * @var ContainerInterface
 */
private $container;

function __construct($container) {
    $this->container = $container;
}

How do you register your Listener? You should register Listener like Service

Like that

core.exceptlistener:
  class: %core.exceptlistener.class%
  arguments: [@service_container]
  tags:
        - { name: kernel.event_listener, event: kernel.exception, method: onKernelException, priority: 200 }

The best way is don't use service_container. The best way is register only necessary services.

Like that

/**
 *
 * @var Twig_Environment
 */
private $twig;

function __construct($twig) {
    $this->twig = $twig;
}
Sign up to request clarification or add additional context in comments.

2 Comments

This was what I needed thanks. The key was setting up the constructor, and then passing the 'arguments' into the service. I can now access the dependencies using $globalVars = $this->container->get('globalvars');
Can you tell, how to render custom error twig only when environment is production?
0

The way I did to solve similar issue is: I dont know if it can help you out.

const GENERIC_CODE = 550;
public function onKernelException(GetResponseForExceptionEvent $event)
{
    $exception = $event->getException();
    if ($exception instanceof RedirectableException) {
        $request = $event->getRequest();
        $url = $exception->getUrl($this->_authentication['base']['url']);
        $response = new Response();
        $response->setStatusCode(self::GENERIC_CODE, AuthenticationException::GENERIC_MESSAGE);
        $response->setContent($url);
        $event->setResponse($response);
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.