Symfony2: Custom Error Page Extend base.html.twig
Asked Answered
B

6

7

I am trying to customize the error pages in Symfony.

This is my error.html.twig file located in app/Resources/TwigBundle/views/Exception/:

{% extends '::base.html.twig' %}

{% block body %}

<h1>{{ status_code }}: {{ status_text }}</h1>

{% endblock %}

Unfortunately I get the following error message:

Fatal error: Uncaught exception 'Symfony\Component\Routing\Exception\ResourceNotFoundException' in [...] vendor\symfony\symfony\src\Symfony\Component\HttpKernel\EventListener\RouterListener.php on line 144

When I remove {% extends '::base.html.twig' %} everything works fine. Any ideas how to have my base template included in the error page?

Edit 1: The strange thing is that it seems to work when a 403 is thrown, e.g., when I access /user but don't have the necessary privilege.

Edit 2: I pasted the whole content of my base.html.twig into the error.html.twig file and noticed that the error was caused due to the menu rendered by the KnpMenuBundle bundle:

{{ knp_menu_render('ACMEMemberBundle:Builder:mainMenu', { 'style': 'pills', 'currentClass': 'active' }) }}

When I remove this line, everything works fine. But this is not the way I would like to go. Is there no possibility to keep the navigation?

Balm answered 1/3, 2014 at 8:0 Comment(3)
#12635461Madeline
@abbiya Thanks for your comment. If you are referring to the production environment, I am actually running my application in the production mode.Balm
I had a similar issue. The problem there was using path with the current request's route. 404 error does not have a route.Pipestone
N
1

file should be located in app/Resources/views/Exception/

instead of

app/Resources/TwigBundle/views/Exception/

Nit answered 3/5, 2017 at 8:13 Comment(0)
D
0

Did you put the page in the following place?

/app/ResourceS/TwigBundle/views/Exception/error404.html.twig

{% extends '::base.html.twig' %}
{% block content %}
{%trans%}errors.404{%endtrans%}
 {% endblock %} 
Defeatism answered 18/5, 2014 at 16:25 Comment(0)
S
0
// app/Resouces/views/base.html.twig

×
{% include('path/to/include') %}

○
{% include('::path/to/include') %}
Stalder answered 24/12, 2014 at 10:37 Comment(0)
E
0

I finally have done it putting the whole code (base+current) in the same 'error.html.twig' file.

It works for me and avoided a huge headache.

Enesco answered 29/1, 2016 at 12:2 Comment(0)
K
0

plz remove :: in first line https://symfony.com/doc/current/templating.html

 {% extends 'base.html.twig' %}

{% block body %}

<h1>{{ status_code }}: {{ status_text }}</h1>

{% endblock %}
Krugersdorp answered 9/12, 2017 at 19:26 Comment(0)
S
0

I see that this question doesnt have an accepted answer. But the answer to this question is in the comments under original post by mark.sagikazar.

If your error404 crashes after you put

{% extends 'base.html.twig' %}

into your error404.html.twig with error saying:

Symfony\Bridge\Twig\Extension\RoutingExtension::getPath(): Argument #1 ($name) must be of type string, null given, called in

Then the problem lies in the base.html.twig. In my case it was

app.request.attributes.get('_route')

Because like mark.sagikazar said there is no route in error 404 nor other error messages.

So i just put an if statement around this code like this:

{% if app.request.attributes.get('_route') %}
.. do something here that doesnt work in error404.html twig
{% endif %}
Stig answered 11/10, 2023 at 9:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.