自定义错误页面模板

自定义错误页面模板

本文介绍了如何在symfony 3.1中自定义错误页面模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Symfony 3.1.我确实按照下面的网址实施了自定义错误页面模板.

I am using Symfony 3.1. I did follow below url for implementing the custom error page template.

( http://symfony.com/doc/current/controller/error_pages.html#custom-exception-controller ).

(http://symfony.com/doc/current/controller/error_pages.html#custom-exception-controller).

我在同一位置创建所有文件:

I creates all files under the same location:

app/
└─ Resources/
   └─ TwigBundle/
      └─ views/
         └─ Exception/
            ├─ error404.html.twig
            ├─ error403.html.twig
            ├─ error.html.twig      # All other HTML errors (including 500)
            ├─ error404.json.twig
            ├─ error403.json.twig
            └─ error.json.twig      # All other JSON errors (including 500)

我确实创建了一个名称为"TwigBundle"的捆绑软件,并创建了名称为"TwigBundle \ Controller \ ExceptionController.php"的控制器文件夹和类

I did create a bundle with name "TwigBundle" and create controller folder and class with name "TwigBundle\Controller\ExceptionController.php"

Config.yml

twig:
    exception_controller:  TwigBundle:ExceptionController:showException

error.html.twig

    <h1>Page not found</h1>

    {% if is_granted('IS_AUTHENTICATED_FULLY') %}
        {# ... #}
    {% endif %}

    <p>
        The requested page couldn't be located. Checkout for any URL
        misspelling or <a href="{{ path('homepage') }}">return to the homepage</a>.
    </p>

In case you need them, the Ex

我收到以下错误消息:

Fatal error: Uncaught exception 'Symfony\Component\Routing\Exception\ResourceNotFoundException' in D:\xampp\htdocs\PROJECT_NAME\var\cache\prod\appProdUrlMatcher.php:922 Stack trace: #0 D:\xampp\htdocs\PROJECT_NAME\var\cache\prod\classes.php(1744): appProdUrlMatcher->match('/jkhjjkohuioioi...') #1 D:\xampp\htdocs\PROJECT_NAME\var\cache\prod\classes.php(1613): Symfony\Component\Routing\Matcher\UrlMatcher->matchRequest(Object(Symfony\Component\HttpFoundation\Request)) #2 D:\xampp\htdocs\PROJECT_NAME\var\cache\prod\classes.php(2758): Symfony\Component\Routing\Router->matchRequest(Object(Symfony\Component\HttpFoundation\Request)) #3 [internal function]: Symfony\Component\HttpKernel\EventListener\RouterListener->onKernelRequest(Object(Symfony\Component\HttpKernel\Event\GetResponseEvent), 'kernel.request', Object(Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher)) #4 D:\xampp\htdocs\PROJECT_NAME\vendor\symfony\symfony\src\Symfony\Component\EventDispatcher\Debug\WrappedListener.php(61): call_user_func(Array, Object(Symfony\Component\HttpKernel\ in D:\xampp\htdocs\PROJECT_NAME\var\cache\prod\classes.php on line 3328

我已清除缓存,并且系统正在Prod ENV中运行.但是我仍然没有得到自定义错误模板.

I have clear the cache and system is running in Prod ENV.But still i am not getting custom error template.

推荐答案

首先,您需要在以下位置创建新的错误模板:/YOUR_PROJECT_FOLDER/app/Resources/TwigBundle/views/Exception/

In first, you need to create new errors templates into this location:/YOUR_PROJECT_FOLDER/app/Resources/TwigBundle/views/Exception/

作为示例,让我们创建一个: error.html.twig 处理所有错误

For sample, let create one: error.html.twig for handle all errors

第二秒,您必须为错误视图编写一些模板内容.为了进行实验,请将这些内容放入我们的error.html.twig:

In second you must write some template content for your errors views.For the experiment, let put this content into our error.html.twig:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Error test. An Error Occurred: {{ status_text }}</title>
</head>
<body>
<h1>WooHoo! Now you can see new error template</h1>
<h2>The server returned a "{{ status_code }} {{ status_text }}".</h2>
</body>
</html>

第三,让我们在开发环境中检查此错误.为此,请将此路由放入您的开发路由文件/YOUR_PROJECT_FOLDER/app/config/routing_dev.yml (可能已经存在)

In third, let check this error in dev environment. To do this put this route into your dev route file /YOUR_PROJECT_FOLDER/app/config/routing_dev.yml (it can be there already)

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

添加此路由后,让我们检查新的错误模板.在您的网络浏览器中转到此链接:

After adding this route, let's check our new error template. go to this link in your web browser:

/app_dev.php/_error/418 其中 418 只是错误状态代码

/app_dev.php/_error/418 where is 418 is just error status code

如果在开发环境中看到新的错误模板,则在prod env上也将是相同的.如果要在生产环境中检查缓存,请不要忘记清除缓存.

If you see new error template in develop environment it will be the same on prod env too. Do not forget to clear cache if you want to check it on production environment.

请注意,您可以创建模板 error404.html.twig 来仅处理404错误.或者,您可以为XML错误500错误创建 error500.xml.twig .

Note that you can create template error404.html.twig for handle only 404 errors. Or you can create error500.xml.twig for xml reponse of 500 error.

这篇关于如何在symfony 3.1中自定义错误页面模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 11:36