为什么在升级到 Symfony 4.4 后我不再看到错误预览页面?

Why after upgrading to Symfony 4.4 I no longer get the error preview pages?

我刚刚将一个项目从 Symfony 4.3 更新到 4.4。更新后,当我出现错误时,显示的页面是带有 "Oops! An Error Occurred!" 的生产错误页面,而不是带有所有错误痕迹的开发错误页面。

此外,探查器不记录错误页面,我可以在探查器中看到所有请求,但看不到有错误的请求。

如果我查看日志(我正在使用 docker),我可以看到 php 错误:

$ docker logs php
172.21.0.3 -  17/Jun/2020:09:50:53 +0000 "GET /index.php" 500
[17-Jun-2020 09:50:53] WARNING: [pool www] child 6 said into stderr: "NOTICE: PHP message: PHP Fatal error:  Uncaught Twig\Error\SyntaxError: Unexpected "}". in /app/templates/professionals/artists/list.html.twig:26"
[17-Jun-2020 09:50:53] WARNING: [pool www] child 6 said into stderr: "Stack trace:"
[17-Jun-2020 09:50:53] WARNING: [pool www] child 6 said into stderr: "#0 /app/vendor/twig/twig/src/Lexer.php(292): Twig\Lexer->lexExpression()"
[17-Jun-2020 09:50:53] WARNING: [pool www] child 6 said into stderr: "#1 /app/vendor/twig/twig/src/Lexer.php(186): Twig\Lexer->lexVar()"
[17-Jun-2020 09:50:53] WARNING: [pool www] child 6 said into stderr: "#2 /app/vendor/twig/twig/src/Environment.php(542): Twig\Lexer->tokenize(Object(Twig\Source))"
[17-Jun-2020 09:50:53] WARNING: [pool www] child 6 said into stderr: "#3 /app/vendor/twig/twig/src/Environment.php(595): Twig\Environment->tokenize(Object(Twig\Source))"
[17-Jun-2020 09:50:53] WARNING: [pool www] child 6 said into stderr: "#4 /app/vendor/twig/twig/src/Environment.php(408): Twig\Environment->compileSource(Object(Twig\Source))"
[17-Jun-2020 09:50:53] WARNING: [pool www] child 6 said into stderr: "#5 /app/vendor/twig/twig/src/Environment.php(381): Twig\Environment->loadClass('__TwigTemplate_...', 'professionals/a...', NULL)"
[17-Jun-2020 09:50:53] WARNING: [pool www] child 6 said into stderr: "#6 /app/vendor/twig/twig/src/Environment.php(359): Twig\Environment->loadTemplate('professionals/a...')"
[17-Jun-2020 09:50:53] WARNING: [pool www] child 6 said into stderr: "#7 /app/vendor/symfony/twig-bridge/TwigEngine.php(135): Twig\Environment->load('professionals/a...')"
[17-Jun-2020 09:50:53] WARNING: [pool www] child 6 said into stderr: "#8 /app/vendor/symfony/twig-bridge/TwigEngine.php(54): Symfony\Bridge\Twig\TwigEngine->load(..."

.env 文件设置为 APP_ENV=dev

在 Symfony 4.4 中发布了 ErrorHandler 组件,that replaced the Debug component

因此,一些文件的位置发生了变化。

您需要找到 config/routes/dev/twig.yaml,然后删除这些行:

# config/routes/dev/twig.yaml
_errors:
    resource: '@TwigBundle/Resources/config/routing/errors.xml'
    prefix:   /_error

取而代之,创建一个新文件 config/routes/dev/framework.yaml,内容如下:

# config/routes/dev/framework.yaml
_errors:
    resource: '@FrameworkBundle/Resources/config/routing/errors.xml'
    prefix:   /_error

这应该会在开发期间恢复旧的错误预览页面。

发现错误。这是包 FosRestBundle 配置错误。

我有 friendsofsymfony/rest-bundle 2.5 版。我升级到 2.8 并在 fos_rest-yml 中删除了配置 "exception_controller: 'fos_rest.exception.controller:showAction"。

之后一切正常。

Debug class 已从 Symfony\Component\Debug\Debug 更改 到 Symfony 4.4

中的 Symfony\Component\ErrorHandler\Debug

因此您必须更改 app_dev.php 和其他调试前端控制器,以便 Debug::enable() 通向新控制器。

https://github.com/symfony/symfony/issues/36254#issuecomment-701260396