4

I'm trying to customize error templates on my Symfony 5 project. I want to use the TwigBundle error renderer because it seems to be the lightest solution.

So I followed this doc : https://symfony.com/doc/current/controller/error_pages.html#overriding-the-default-error-templates

TwigBundle looks like correctly installed as I run this command : composer require symfony/twig-pack without any error.

But now I can't find any error.html.twig in any folder of the TwigBundle, especially in the TwigBundle/Exception/ folder (which doesn't even exist) as suggested in the documentation.

Maybe this is not the good "TwigBundle" I installed ? Or maybe there is a better and lighter solution to customize error pages ?

gp_sflover
  • 3,460
  • 5
  • 38
  • 48
Rémi Leblanc
  • 33
  • 1
  • 3
  • Then why not add such a template file? The documentation does not state anywhere that such a file exists already – Nico Haase Apr 02 '20 at 13:57
  • 1
    Oh sorry, my original post miss something, actually the default error template used by symfony is in the (default ?) 'error-handler' bundle in a file called 'error.html.php', and it's used for any error. So adding a file in the twig bundle won't change anything as it's not even used. – Rémi Leblanc Apr 02 '20 at 14:10
  • the original is actually here: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/ErrorHandler/Resources/views/error.html.php but you override it according to the docs – craigh Apr 02 '20 at 16:32
  • yeah I found this one, that's the problem actually. The default template should be 'error.html.twig' in the twig-bundle folder. But symfony isn't using the twig error renderer – Rémi Leblanc Apr 02 '20 at 17:57
  • 1
    Please, use the answer section if you've a solution to post to help other users to find your answer – gp_sflover May 27 '20 at 11:06

3 Answers3

6

You need to overwrite the errors templates Like -> In your templates folder you create folders like this templates -> bundles->TwigBundle->Exception In the exception folder you can create one file for every type of error like error404.html.twig OR error403.html.twig error.html.twig # All other HTML errors (including 500)

templates/
 └─ bundles/
  └─ TwigBundle/
    └─ Exception/
      ├─ error404.html.twig
      ├─ error403.html.twig
      └─ error.html.twig      # All other HTML errors (including 500)

Notice that this error page will be displayed just in prod

Source : https://symfony.com/doc/current/controller/error_pages.html

Nadim Al Abdou
  • 768
  • 7
  • 13
1

As Nadim Ai Abdou's answer,but in my environment(php8+nginx in docker) there is a extra step to clear cache

the steps

  • set APP_ENV=prod in .env file
  • run bin/console cache:clear clear cache

I'm a beginner to symfony framework andI have no ideal why symfony5's docs not words about the env and cache,and can't work correctly like docs

1

All according to the documentation https://symfony.com/doc/current/controller/error_pages.html AND

  1. Set productive environment in .env file APP_ENV=prod
  2. Сlear cache run: php bin/console cache:clear
  3. If the template error.html.twig contains errors or functions for a dev environment (such as dump()), then a standard symfony error template from vendor/symfony/error-handler will be displayed.