How to modify error page in Symfony2

It is very important for a website to customize the error page i.e error 404 page.Today I will tell you how to modify error page in Symfony2. When there is a error in symfony2 an exception is generated. The framework shows a detail information about the error. But when we put are application in production environment, we will need a very friendly error message page or a sitemap page.

You need to create a page and a controller that will generate a response.In my application, we are creating a ExceptionController.

The Execption Controller looks like

<?php namespace Dev\DreamBundle\Controller;
use Symfony\Component\HttpKernel\Exception\FlattenException;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;

class ExceptionController extends \Symfony\Bundle\FrameworkBundle\Controller\Controller
{
/**
* Converts an Exception to a Response.
*
* @param FlattenException $exception A FlattenException instance
* @param DebugLoggerInterface $logger A DebugLoggerInterface instance
* @param string $format The format to use for rendering (html, xml, …)
* @param Boolean $embedded Whether the rendered Response will be embedded or not
*
* @throws \InvalidArgumentException When the exception template does not exist
*/
public function exceptionAction(FlattenException $exception, DebugLoggerInterface $logger = null, $format = ‘html’, $embedded = false)
{
$arraytopass= array(‘exception’ => $exception);
return $this->render(‘DevDreamBundle:Exception:error.html.twig’,$arraytopass);
}

}
?>
The Resources/views/Exception/error.html.twig template would look like:

{% block body %}
<div>Thank you for reading this tutorial………
customized error page
</div>
{% endblock %}

For the final step you need to tell Symfony to use your controller to handle exceptions. Set the following in config_prod.yml:
Symfony/app/config/config_prod.yml

parameters:
exception_listener.controller: “DevDreamBundle:Exception:exception”

After doing all this please clear your cache(Very Important Step)
command is “app/console cache:clear”

You are good to go…….

Advertisements