如何在Symfony2开发环境中显示404错误页面

问题描述 投票:12回答:4

我想在开发环境中处理404页面。我使用此文件自定义404:app / Resources / TwigBundle / views / Exception / error.html.twig

这个产品网址正常工作:mysite.com/404

但是这一个mysite.com/app_dev.php/404抛出NotFoundHttpException并给我开发调试页面。

是否可以显示错误页面而不是调试页面?

更新:

官方文档现在有一章关于:Testing Error Pages during Development

symfony development-environment
4个回答
12
投票

在开发环境中使用As of Symfony2.6,您可以使用以下路由:

/_error/404.html

其中404是测试的错误代码,html是请求的格式。为了能够使用此功能,请确保在routing_dev.yml文件中包含以下条目:

# app/config/routing_dev.yml
_errors:
    resource: "@TwigBundle/Resources/config/routing/errors.xml"
    prefix:   /_error

19
投票

要显示错误页面,请在web / app_dev.php中将第二个参数更改为false

$kernel = new AppKernel('dev', false);

在测试了您需要的内容后,请将其更改回来。


UPDATE

感谢@ user2019515指出 - 现在(2.3及以上)有WebfactoryExeptionsBundleSymfony docs的链接,我上面写的方法不应该使用。


13
投票

您需要在开发时覆盖exception_full.html.twig模板。

app/Resources/TwigBundle/views/Exception/exception_full.html.twig

Symfony2使用此模板在开发期间为您提供尽可能多的调试信息。

当内核处于调试模式时,Symfony2将使用exception_full.html.twig,否则它将使用您覆盖的特定模板。

有关更多详细信息,请参阅vendor / symfony / src / Symfony / Bundle / TwigBundle / Controller / ExceptionController.php,特别是showAction()和findTemplate()函数。


0
投票

您还可以向routing_dev.yml文件添加路由

error404:
    path: /404
    defaults:
        _controller: FrameworkBundle:Template:template
        template:    TwigBundle:Exception:error404.html.twig

error500:
    path: /500
    defaults:
        _controller: FrameworkBundle:Template:template
        template:    TwigBundle:Exception:error500.html.twig
© www.soinside.com 2019 - 2024. All rights reserved.