簡體   English   中英

Symfony2:自定義錯誤頁面擴展base.html.twig

[英]Symfony2: Custom Error Page Extend base.html.twig

我試圖在Symfony中自定義錯誤頁面。

這是我在app/Resources/TwigBundle/views/Exception/ error.html.twig文件:

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

{% block body %}

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

{% endblock %}

不幸的是我收到以下錯誤消息:

致命錯誤:第144行上的vendor \\ symfony \\ symfony \\ src \\ Symfony \\ Component \\ HttpKernel \\ EventListener \\ RouterListener.php中未捕獲的異常'Symfony \\ Component \\ Routing \\ Exception \\ ResourceNotFoundException'

當我刪除{% extends '::base.html.twig' %}一切正常。 有關如何將我的基本模板包含在錯誤頁面中的任何想法?

編輯1:奇怪的是它似乎在拋出403時工作,例如,當我訪問/user但沒有必要的權限時。

編輯2:我將base.html.twig的全部內容粘貼到error.html.twig文件中,發現該錯誤是由於KnpMenuBundle包提供的菜單引起的:

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

當我刪除這一行時,一切正常。 但這不是我想要的方式。 導航沒有可能嗎?

文件應位於app/Resources/views/Exception/

代替

app/Resources/TwigBundle/views/Exception/

您是否將頁面放在以下位置?

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

{% extends '::base.html.twig' %}
{% block content %}
{%trans%}errors.404{%endtrans%}
 {% endblock %} 
// app/Resouces/views/base.html.twig

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

○
{% include('::path/to/include') %}

我終於完成了將整個代碼(base + current)放在同一個'error.html.twig'文件中。

它對我有用,避免了一個巨大的頭痛。

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 %}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM