Reputation: 71
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?
Upvotes: 7
Views: 2297
Reputation: 257
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 %}
Upvotes: 0
Reputation: 1680
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 %}
Upvotes: 0
Reputation: 31
file should be located in app/Resources/views/Exception/
instead of
app/Resources/TwigBundle/views/Exception/
Upvotes: 1
Reputation: 717
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.
Upvotes: 0
Reputation: 1
// app/Resouces/views/base.html.twig
×
{% include('path/to/include') %}
○
{% include('::path/to/include') %}
Upvotes: 0
Reputation: 2949
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 %}
Upvotes: 0