How to override errors templates for Symfony

881 Views Asked by At

I want to override the error templates in Symfony 3. I've started by creating a TwigBundle folder withe the personalised twig.

app/
 └─ Resources/
    └─ TwigBundle/
      └─ Exception/
        ├─ error404.html.twig
        ├─ error403.html.twig
        ├─ error.html.twig   

Then I checked the routing_dev file which contains this lines :

_errors:
resource: "@TwigBundle/Resources/config/routing/errors.xml"
prefix:   /_error

Symfony still displays the default error templates. I want to know if I should verify something else.

PS : I have already checked this documentation : How to Customize Error Pages

EDIT :

enter image description here

1

There are 1 best solutions below

2
On

The overriden files should reside in a subfolder named views. The correct file tree would look like this:

app/
 └─ Resources/
    └─ TwigBundle/
        └─ views/
            └─ Exception/
                ├─ error404.html.twig
                ├─ error403.html.twig
                ├─ error.html.twig  

Clear your cache afterwards.

Be aware that those overriden error templates do NOT show up in the dev environment.

If you want to test the templates in your dev environment you must ensure you have imported the following in routing_dev.yml:

_errors:
    resource: "@TwigBundle/Resources/config/routing/errors.xml"
    prefix:   /_error

Now you can access /_error/<error-code>.html (i.e. localhost:8000/_error/404.html with the integrated webserver)