Friday, October 9, 2015

How To Customize 404 Error Page In Symfony 2?

In Symfony applications, all errors are treated as exceptions, no matter if they are just a 404 Not Found error or a fatal error triggered by throwing some exception in your code.

To override the 404 error template for HTML pages, create a new error404.html.twig template located at app/Resources/TwigBundle/views/Exception/:
{# app/Resources/TwigBundle/views/Exception/error404.html.twig #}
{% extends 'base.html.twig' %}

{% block body %}
    <h1>Page not found </h1>
    {{ exception.message }}
        The requested page couldn't be located. Checkout for any URL
        misspelling or <a href="{{ path('homepage') }}">return to the homepage</a>.
{% endblock %}
You can access your detected text with following way:
{{ exception.message }}
If error404.html.twig template is not created, then it will fall back to the generic HTML template app/Resources/TwigBundle/views/Exception/error.html.twig.

Do not forget to clear the cache after saving the error404.html.twig template. You can clear cache via
$ php app/console cache:clear

//to clear cache in production environment
$ php app/console cache:clear --env:prod
You can read more detail about How to customize error pages in Symfony 2 => How to Customize Error Pages

1 comment:

  1. It doesn't work :( . Still the old 404 page is there