Surcharger les pages d’erreurs (404) et autres dans symfony

C’est super simple !

Suiviez le guide, il suffit de surcharger les templates twig prévu à cet effet, en recréeant les même fichier et arboresence dans :

app/
└─ Resources/
   └─ TwigBundle/
      └─ views/
         └─ Exception/
            ├─ error404.html.twig
            ├─ error403.html.twig
            ├─ error.html.twig      # All other HTML errors (including 500)
            ├─ error404.json.twig
            ├─ error403.json.twig
            └─ error.json.twig      # All other JSON errors (including 500)

Petit bug ?

Personnellement j’ai eu un bug quand j’ai étendu avec mon template base.html.twig, car j’utilisais le code {% if is_granted("IS_AUTHENTICATED_REMEMBERED") %} dans ce template.

Il suffit de remplacer ce code par {% if app.user and is_granted("IS_AUTHENTICATED_REMEMBERED") %}

Voici l’explication provenant de la documentation de symfony:

You must not use is_granted in your error pages (or layout used by your error pages), because the router runs before the firewall. If the router throws an exception (for instance, when the route does not match), then using is_granted will throw a further exception. You can use is_granted safely by saying {% if app.user and is_granted(‘…’) %}.

 

Un peu de fun

Pour rendre la page 404 un peu plus fun et égayé un peu le visiteur, je vous propose de mettre un petit gif animé.

Un petit sourire, ça fait du bien à tout le monde

renard

jungle