【问题标题】:How to Customize Error Pages Template In symfony 3.1?如何在 symfony 3.1 中自定义错误页面模板?
【发布时间】:2016-10-26 08:45:21
【问题描述】:

我正在使用 Symfony 3.1。我确实按照下面的 url 来实现自定义错误页面模板。

(http://symfony.com/doc/current/controller/error_pages.html#custom-exception-controller)。

我在同一位置创建所有文件:

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)

我确实创建了一个名为“TwigBundle”的包,并创建了一个名为“TwigBundle\Controller\ExceptionController.php”的控制器文件夹和类

Config.yml

树枝配置

twig:    
    exception_controller:  TwigBundle:ExceptionController:showException

error.html.twig

    <h1>Page not found</h1>

    {% if is_granted('IS_AUTHENTICATED_FULLY') %}
        {# ... #}
    {% endif %}

    <p>
        The requested page couldn't be located. Checkout for any URL
        misspelling or <a href="{{ path('homepage') }}">return to the homepage</a>.
    </p>

In case you need them, the Ex

我收到以下错误消息:

Fatal error: Uncaught exception 'Symfony\Component\Routing\Exception\ResourceNotFoundException' in D:\xampp\htdocs\PROJECT_NAME\var\cache\prod\appProdUrlMatcher.php:922 Stack trace: #0 D:\xampp\htdocs\PROJECT_NAME\var\cache\prod\classes.php(1744): appProdUrlMatcher->match('/jkhjjkohuioioi...') #1 D:\xampp\htdocs\PROJECT_NAME\var\cache\prod\classes.php(1613): Symfony\Component\Routing\Matcher\UrlMatcher->matchRequest(Object(Symfony\Component\HttpFoundation\Request)) #2 D:\xampp\htdocs\PROJECT_NAME\var\cache\prod\classes.php(2758): Symfony\Component\Routing\Router->matchRequest(Object(Symfony\Component\HttpFoundation\Request)) #3 [internal function]: Symfony\Component\HttpKernel\EventListener\RouterListener->onKernelRequest(Object(Symfony\Component\HttpKernel\Event\GetResponseEvent), 'kernel.request', Object(Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher)) #4 D:\xampp\htdocs\PROJECT_NAME\vendor\symfony\symfony\src\Symfony\Component\EventDispatcher\Debug\WrappedListener.php(61): call_user_func(Array, Object(Symfony\Component\HttpKernel\ in D:\xampp\htdocs\PROJECT_NAME\var\cache\prod\classes.php on line 3328

我已清除缓存并且系统正在 Prod ENV 中运行。 但我仍然没有得到自定义错误模板。

【问题讨论】:

  • 您是刚刚创建了自定义模板还是配置了自定义错误控制器?您执行了哪些具体步骤?
  • 我只是在上面提到的位置创建自定义模板。注意为控制器做的事情
  • 您是否将@TwigBundle/Resources/config/routing/errors.xml 添加到您的开发路由文件中?你不需要重写默认的异常控制器。
  • 好的@Stephan Yamilov 你能提供完整的例子吗?

标签: symfony error-handling


【解决方案1】:

首先,您需要在此位置创建新的错误模板: /YOUR_PROJECT_FOLDER/app/Resources/TwigBundle/views/Exception/

作为示例,让我们创建一个:error.html.twig 用于处理所有错误

第二次您必须为您的错误视图编写一些模板内容。 为了实验,让我们把这个内容放到我们的error.html.twig中:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Error test. An Error Occurred: {{ status_text }}</title>
</head>
<body>
<h1>WooHoo! Now you can see new error template</h1>
<h2>The server returned a "{{ status_code }} {{ status_text }}".</h2>
</body>
</html>

第三个,让我们在开发环境中检查这个错误。为此,将此路由放入您的开发路由文件/YOUR_PROJECT_FOLDER/app/config/routing_dev.yml(它可能已经存在)

# app/config/routing_dev.yml
_errors:
    resource: "@TwigBundle/Resources/config/routing/errors.xml"
    prefix:   /_error

添加此路由后,让我们检查一下我们的新错误模板。在您的网络浏览器中转到此链接:

/app_dev.php/_error/418 哪里是418 只是错误状态码

如果您在开发环境中看到新的错误模板,它在 prod 环境中也将相同。如果您想在生产环境中检查缓存,请不要忘记清除缓存。

请注意,您可以创建模板 error404.html.twig 以仅处理 404 错误。或者您可以为 500 错误的 xml 响应创建 error500.xml.twig。

【讨论】:

  • 在你的开发环境中输入这条路线/app_dev.php/_error/418时,你能粘贴探查器路线信息吗?像这样:joxi.ru/5mdoo7YudvLor1
  • 糟糕,好像出了点问题。 classes.php 第 2031 行中的 1/1 FatalErrorException:编译错误:无法在 classes.php 第 2031 行中重新声明类 Symfony\Component\Debug\ErrorHandler
  • 在 app/config/routing_dev.yml errors.xml 中,我没有任何 errors.xml 文件
  • 我希望你从配置文件中删除twig: exception_controller:参数?
  • 我删除了。现在我只有: twig: debug: "%kernel.debug%" strict_variables: "%kernel.debug%"
【解决方案2】:

对我来说,这是完美无缺的。可能不是文档推荐的方式,但它很容易理解。您可以尝试一下。

\app\Resources\TwigBundle\views\Exception\error.html.twig

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

{% block title %}An Error Occurred: {{ status_text }}{% endblock %}

{% block main %}

    {% if status_code >= 500 %}
        {# twig include 500s server error template here #}
    {% elseif status_code >= 400 %}
        {# twig include 400s user errors template here #}
    {% endif %}

{% endblock %}

【讨论】:

  • 什么结果,同样的致命错误还是别的什么?
  • 当我运行这个时:localhost/project/app_dev.php/_error/418 输出:哎呀,好像出了点问题。 classes.php 第 2031 行中的 1/1 FatalErrorException:编译错误:无法在 classes.php 第 2031 行中重新声明类 Symfony\Component\Debug\ErrorHandler
  • 我为您编写的解决方案不需要创建新的捆绑包、控制器或服务,这在您开始时可能会有点复杂。 (暂时)删除您的自定义并将上述文件按原样放在所述目录中并查看它是否有效
  • 我想我找到了你的问题,你在 1 中混合了 2 种方法。你给 symfony 文档的链接列出了选项 1,2 或 3 而不是步骤 1,2 和 3。所以简单删除 twig: exception_controller: TwigBundle:ExceptionController:showException 一切都会正常。
  • 好的,如果我尝试运行此 url localhost/projectname/abc abc 操作不会退出,那么它将显示自定义模板。没有显示。它显示 No route found for "GET /abc" 404 Not Found - NotFoundHttpException 1 linked Exception: ResourceNotFoundException »
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-27
  • 2014-11-10
  • 1970-01-01
  • 2013-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多