symfony - 如何在 Symfony4 中显示 404 错误页面而不是错误
问题描述
这是我的页面:error404.html.twig 作为文档示例 404 错误模板
{# templates/bundles/TwigBundle/Exception/error404.html.twig #}
{% extends 'base.html.twig' %}
{% block body %}
<h1>Page not found</h1>
<p>
The requested page couldn't be located. Checkout for any URL
misspelling or <a href="{{ path('homepage') }}">return to the homepage</a>.
</p>
{% endblock %}
这是我的页面 config/routes/dev/twig.yaml
# config/routes/dev/twig.yaml
_errors:
resource: '@TwigBundle/Resources/config/routing/errors.xml'
prefix: /_error
这是我的页面 config/packages/twig.yaml
# config/packages/twig.yaml
twig:
exception_controller: App\Controller\ExceptionController::showException
我应该怎么做才能使它起作用?
- 顺便说一句,我使用了第一种方法:覆盖默认错误模板。
解决方案
如果您处于调试模式,而不是 404 页面,NotFoundHttpException
那么一切都很好,因为这是记录和期望的行为,如下所述:https://symfony.com/doc/current/controller/error_pages。 html
在开发环境中,Symfony 捕获所有异常并显示一个特殊的异常页面
要绕过这个并实际查看您的自定义 404 页面,您需要通过特殊路径访问模板:http://localhost/index.php/_error/404
推荐阅读
- c# - 具有日期时间字段的实体框架并发
- r - 如何用 tsibble 和 fable 指定服务时间的间隔或频率?
- javascript - 猫鼬递归查找数据库中的元素
- sql - 逐行计算记录和一些计算 SQL Server
- python - Rebot模型在机器人框架中生成两个log.html文件时出错
- python - 在打印函数中嵌套for循环以在python中打印特定模式
- jenkins - 我如何告诉 Jenkins 构建 OWASP ZAP 的最新稳定版本
- python-3.x - Matplotbib - PercentFormatter - 如何缩放 y 轴并保存绘图?
- c# - 在构建 .NET Core Api 时,将控制器 hepler 方法放置在何处以与设计模式保持一致?
- javascript - 我在节点 js 代码中收到错误意外结束输入