Symfony2:自定义错误页面扩展 base.html.twig [英] Symfony2: Custom Error Page Extend base.html.twig

查看:28
本文介绍了Symfony2:自定义错误页面扩展 base.html.twig的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自定义 Symfony 中的错误页面.

I am trying to customize the error pages in Symfony.

这是我位于 app/Resources/TwigBundle/views/Exception/ 中的 error.html.twig 文件:

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:

致命错误:未捕获的异常'Symfony\Component\Routing\Exception\ResourceNotFoundException' 中[...]供应商\symfony\symfony\src\Symfony\Component\HttpKernel\EventListener\RouterListener.php在线 144

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

当我删除 {% extends '::base.html.twig' %} 时,一切正常.任何想法如何将我的基本模板包含在错误页面中?

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

编辑 1:奇怪的是,它似乎在抛出 403 时起作用,例如,当我访问 /user 但没有必要的权限时.

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.

编辑 2:我将 base.html.twig 的全部内容粘贴到 error.html.twig 文件中,并注意到错误是由 呈现的菜单引起的>KnpMenuBundle 包:

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?

推荐答案

文件应该位于app/Resources/views/Exception/

代替

app/Resources/TwigBundle/views/Exception/

这篇关于Symfony2:自定义错误页面扩展 base.html.twig的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆