如何显示自定义的 404 错误页面 - Symfony2 [英] How to show customized 404 error page - Symfony2
问题描述
我的控制器中有这个:
if(some stuff) {
throw $this->createNotFoundException('message');
}
当我在 dev env 中测试它时,我得到 Symfony 的页面,告诉我的文本检测到异常,但我无法在 prod env 中测试它:( 我运行这个 php app/console cache:clear --env=prod --no-debug
然后只将 URL app_dev.php
替换为 app.php
但工具栏和 Symfony 的错误页面会保留.
When I test it in dev env, I get the Symfony's page telling Exception detected with my text, but I can't test it in prod env :( I run this php app/console cache:clear --env=prod --no-debug
and then replace only in the URL app_dev.php
with app.php
but the toolbar and Symfony's error page stay.
我创建了这个文件 - app/Resources/TwigBundle/views/Exception/error.html.twig
.那么它会在 prod 中呈现吗?
I created this file - app/Resources/TwigBundle/views/Exception/error.html.twig
. So will it be rendered in prod?
就在里面:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>An Error Occurred: {{ status_text }}</title>
</head>
<body>
<h1>Oops! An Error Occurred</h1>
<h2>The server returned a "{{ status_code }} {{ status_text }}".</h2>
</body>
</html>
我应该在哪里为 status_code
和 status_text
赋值?或者它们是从异常中取出的?
where should I give values for status_code
and status _text
? Or they are taken from the exception?
所以总的来说,我想做的是当控制器动作中的条件为真时,我的自定义错误页面将显示在 prod env 中.我已经成功了吗?如果没有,如何制作以及如何在 prod env 中查看结果?
So in general what I want to do is when a condition in my contoller`s action is true, mine customized error page to be shown in prod env. Did I make it already, and if not, how to make it and how to see the result in prod env?
推荐答案
我也尝试过,我注意到放置在 app/Resources/TwigBundle/views/Exception/error.html.twig 中的自定义错误页面只能在 prod 环境中工作.
I also tried and i noticed that custom error pages placed in app/Resources/TwigBundle/views/Exception/error.html.twig worked only in prod environment.
这篇关于如何显示自定义的 404 错误页面 - Symfony2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!