【问题标题】:Differing error templates for dev and production in Symfony2Symfony2 中用于开发和生产的不同错误模板
【发布时间】:2015-03-28 19:36:59
【问题描述】:

按照文档http://symfony.com/doc/current/cookbook/controller/error_pages.html,我已经能够添加自己的错误模板;

app
 |- Resources
 |  | - TwigBundle
 |  |  |- view
 |  |  |  |- Exception
 |  |  |  |  |- exception.html.twig
 |  |  |  |  |- error.html.twig
 |  |  |  |  |- error400.html.twig
 |  |  |  |  |- error404.html.twig
 |  |  |  |  |- error500.html.twig
 |  |  |  |- layout.html.twig
 |- config

这很好用,但我如何为我的开发环境保留堆栈跟踪和详细错误?

在生产中我希望使用我自己的模板,在开发环境中我希望使用 Symfony2 自己的模板。

【问题讨论】:

  • 我昨晚也做了同样的事情,据我所知,Symfony2 (v2.5) 仍然抛出带有完整堆栈跟踪的“绿色”外星人页面,而我在 @987654325 @ 模式。但是,如果我移至 prod,则会显示我的自定义错误页面。
  • 对我来说似乎不是这样,嗯。你在app中设置的模板和我一样吗?
  • 同样,我只有error403.html.twig。你在别的东西上使用Symfony 2.6 吗?从2.6 开始,他们通过预览错误页面使一些事情变得更容易。
  • 2.5,我有错误和异常。 403 在你的开发模式下会被覆盖吗?
  • 如果dev 否,但prod 是。在某一时刻,我希望是这样,因为它确实减慢了 error403.html.twig 布局的测试(每次更改后都必须 cache:clear...)

标签: php symfony templates error-handling twig


【解决方案1】:
app
 |- Resources
 |  |- TwigBundle
 |  |  |- view
 |  |  |  |- Exception
 |  |  |  |  |- exception.html.twig
 |  |  |  |  |- error.html.twig
 |  |  |  |  |- error400.html.twig
 |  |  |  |  |- error404.html.twig
 |  |  |  |  |- error500.html.twig
 |  |  |  |- layout.html.twig
 |  |- view
 |  |  |- my_custom_error_layout.html.twig
 |- config

my_custom_error_layout.html.twig

<!DOCTYPE>
<html>
<head>
</head>
<body>
html, block, whatnot go here
</body>
</html>

error400.html.twig

{% extends "::my_custom_error_layout.html.twig" %}

{% block body %}
details or fixed message go here...
{% endblock %}

【讨论】:

    【解决方案2】:

    在我们最近的项目中,我们实现了一个自定义的ExceptionListener,配置如上:

        <!-- Acme Exception Listener -->
        <service id="kernel.listener.customer_area_exception_listener" class="AcmeSecurityBundle\Listener\AcmeExceptionListener">
            <argument type="service" id="templating" />
            <argument>%acme.exceptions.debug%</argument>
            <tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" />
        </service>
    

    带有参数(来自 parameters.yml )用于区分环境中的行为。

    异常侦听器知道如何渲染每种类型的异常,也可以自定义异常。

    希望对你有所帮助

    【讨论】:

      猜你喜欢
      • 2017-07-27
      • 1970-01-01
      • 2023-03-24
      • 2019-11-10
      • 2015-11-18
      • 1970-01-01
      • 2021-05-29
      • 1970-01-01
      • 2016-03-29
      相关资源
      最近更新 更多