symfony - 如何显示 404 错误页面而不是 Symfony4 中的错误

标签 symfony symfony4

这是我的页面:error404.html.twig 作为文档 Example 404 Error Template

{# 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

我应该怎么做才能使它起作用?
  • 顺便说一下,我使用了第一种方法 :覆盖默认错误模板。

  • 错误是: :
    enter image description here

    服务.yaml:
    enter image description here

    最佳答案

    如果您处于 Debug模式并且不是 404 页面,您会得到 NotFoundHttpException那么一切都很好,因为这是记录在案的和期望的行为,如下所述:https://symfony.com/doc/current/controller/error_pages.html

    In the development environment, Symfony catches all the exceptions and displays a special exception page


    要绕过这个并实际看到您的自定义 404 页面,您需要通过特殊途径访问模板:http://localhost/index.php/_error/404

    关于symfony - 如何显示 404 错误页面而不是 Symfony4 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52770358/

    相关文章:

    symfony - 在 Symfony 4 中为路由配置默认主机

    使用 Heroku 部署 Symfony2 : Dumping assets

    php - 没有传输支持给定的 Messenger DSN

    javascript - Symfony 4 带有 jquery 插件和 WebEncore

    validation - Symfony2 验证 - 没有为约束配置默认选项

    php - 在 Symfony 中重定向回两步

    php - 在 Symfony v4.2 中将带有数据的数组传递给 ChoiceType (html select)

    php - 如何使用 Symfony Serializer 反序列化复杂对象数组?

    php - Symfony后台事件发送响应错误 session 后

    php - symfony 2 组权限 Doctrine 问题