我正尝试在我的 Symfony 5 项目上自定义错误模板。我想使用 TwigBundle 错误渲染器,因为它似乎是最简单的解决方案。
所以我遵循了这个文档:https://symfony.com/doc/current/controller/error_pages.html#overriding-the-default-error-templates
当我运行此命令时,TwigBundle 看起来已正确安装:composer require symfony/twig-pack
没有任何错误。
但现在我在 TwigBundle 的任何文件夹中都找不到任何 error.html.twig
,尤其是在 TwigBundle/Exception/
文件夹中(它没有甚至存在)如文档中所建议的那样。
也许这不是我安装的好“TwigBundle”?或者也许有更好更轻便的自定义错误页面的解决方案?
最佳答案
您需要覆盖错误模板 喜欢 -> 在您的模板文件夹中,您创建这样的文件夹 模板 -> 包 -> TwigBundle -> 异常 在异常文件夹中,您可以为每种类型的错误创建一个文件,例如 error404.html.twig 或 error403.html.twig error.html.twig # 所有其他 HTML 错误(包括 500)
templates/
└─ bundles/
└─ TwigBundle/
└─ Exception/
├─ error404.html.twig
├─ error403.html.twig
└─ error.html.twig # All other HTML errors (including 500)
注意这个错误页面只会在prod中显示
来源:https://symfony.com/doc/current/controller/error_pages.html
关于Symfony 5 - 自定义 Twig 错误模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60993170/