如何显示自定义的404错误页面 - Symfony2

时间:2022-10-10 16:54:16

I have this in my controller:

我在我的控制器中有这个:

if(some stuff) {
    throw $this->createNotFoundException('message');
}

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.

当我在dev env中测试它时,我得到了Symfony的页面,告诉我用我的文本检测到Exception,但我无法在prod env中测试它:(我运行这个php app / console cache:clear --env = prod --no -debug然后只在URL app_dev.php中用app.php替换,但工具栏和Symfony的错误页面保持不变。

I created this file - app/Resources/TwigBundle/views/Exception/error.html.twig. So will it be rendered in prod?

我创建了这个文件 - app / Resources / TwigBundle / views / Exception / error.html.twig。它将在生产中呈现吗?

This is in it:

这是在:

<!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>

where should I give values for status_code and status _text? Or they are taken from the exception?

我应该在哪里给出status_code和status _text的值?或者他们是从例外中获取的?

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?

因此,一般来说,我想要做的是当我的控制器操作中的条件为真时,我的自定义错误页面将显示在prod env中。我是否已经成功,如果没有,如何制作它以及如何在prod env中查看结果?

5 个解决方案

#1


16  

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.

我也试过,我注意到app / Resources / TwigBundle / views / Exception / error.html.twig中的自定义错误页面仅在prod环境中有效。

#2


14  

Better late than never I guess..

迟到总比我想的好......

You probably know about overriding of templates. You can override any template you want. So if you want to override the dev error page, just for debugging, then you should override the template:

您可能知道覆盖模板。您可以覆盖所需的任何模板。因此,如果您想覆盖dev错误页面,仅用于调试,那么您应该覆盖模板:

exception_full.html.twig

You can do so by creating such a file in this folder:

您可以通过在此文件夹中创建此类文件来执行此操作:

app/Resources/TwigBundle/views/Exception

Now you will see your customized 404 in dev mode.

现在,您将在开发模式下看到自定义的404。

#3


7  

you can access your detected text with following way:

您可以通过以下方式访问检测到的文本:

{{ exception.message }}

#4


5  

Read the doc http://symfony.com/doc/2.0/cookbook/controller/error_pages.html

阅读文档http://symfony.com/doc/2.0/cookbook/controller/error_pages.html

and Customizing the 404 Page and other Error Pages

和自定义404页面和其他错误页面

http://symfony.com/doc/2.0/cookbook/controller/error_pages.html#customizing-the-404-page-and-other-error-pages

http://symfony.com/doc/2.0/cookbook/controller/error_pages.html#customizing-the-404-page-and-other-error-pages

#5


3  

It appears Symfony has a new way of allowing you to see the error page templates in your dev environment. Taken from their docs:

看起来Symfony有一种新方法允许您在开发环境中查看错误页面模板。取自他们的文档:

While you're in the development environment, Symfony shows the big exception page instead of your shiny new customized error page. So, how can you see what it looks like and debug it?

当您处于开发环境中时,Symfony会显示大异常页面而不是您闪亮的新自定义错误页面。那么,你怎么看到它的样子并进行调试呢?

Fortunately, the default ExceptionController allows you to preview your error pages during development.

幸运的是,默认的ExceptionController允许您在开发期间预览错误页面。

To use this feature, you need to have a definition in your routing_dev.yml file like so:

要使用此功能,您需要在routing_dev.yml文件中有一个定义,如下所示:

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

Read more: Symfony Error Pages Manual

阅读更多:Symfony错误页面手册

#1


16  

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.

我也试过,我注意到app / Resources / TwigBundle / views / Exception / error.html.twig中的自定义错误页面仅在prod环境中有效。

#2


14  

Better late than never I guess..

迟到总比我想的好......

You probably know about overriding of templates. You can override any template you want. So if you want to override the dev error page, just for debugging, then you should override the template:

您可能知道覆盖模板。您可以覆盖所需的任何模板。因此,如果您想覆盖dev错误页面,仅用于调试,那么您应该覆盖模板:

exception_full.html.twig

You can do so by creating such a file in this folder:

您可以通过在此文件夹中创建此类文件来执行此操作:

app/Resources/TwigBundle/views/Exception

Now you will see your customized 404 in dev mode.

现在,您将在开发模式下看到自定义的404。

#3


7  

you can access your detected text with following way:

您可以通过以下方式访问检测到的文本:

{{ exception.message }}

#4


5  

Read the doc http://symfony.com/doc/2.0/cookbook/controller/error_pages.html

阅读文档http://symfony.com/doc/2.0/cookbook/controller/error_pages.html

and Customizing the 404 Page and other Error Pages

和自定义404页面和其他错误页面

http://symfony.com/doc/2.0/cookbook/controller/error_pages.html#customizing-the-404-page-and-other-error-pages

http://symfony.com/doc/2.0/cookbook/controller/error_pages.html#customizing-the-404-page-and-other-error-pages

#5


3  

It appears Symfony has a new way of allowing you to see the error page templates in your dev environment. Taken from their docs:

看起来Symfony有一种新方法允许您在开发环境中查看错误页面模板。取自他们的文档:

While you're in the development environment, Symfony shows the big exception page instead of your shiny new customized error page. So, how can you see what it looks like and debug it?

当您处于开发环境中时,Symfony会显示大异常页面而不是您闪亮的新自定义错误页面。那么,你怎么看到它的样子并进行调试呢?

Fortunately, the default ExceptionController allows you to preview your error pages during development.

幸运的是,默认的ExceptionController允许您在开发期间预览错误页面。

To use this feature, you need to have a definition in your routing_dev.yml file like so:

要使用此功能,您需要在routing_dev.yml文件中有一个定义,如下所示:

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

Read more: Symfony Error Pages Manual

阅读更多:Symfony错误页面手册