Which templates should be overridden in Symfony 2 error customization?

亡梦爱人 提交于 2019-12-18 09:37:51

问题


From the documentation:

All of the error templates live inside TwigBundle. To override the templates, we simply rely on the standard method for overriding templates that live inside a bundle.

And:

To see the full list of default error templates, see the Resources/views/Exception directory of the TwigBundle.

Looking at the after-mentioned directory i can find several files. I'm interested in custom templates for 403, 404 and 500 errors, so i created error.html.twig (parent template) and error403.html.twig, error404.html.twig and error500.html.twig that extends from 'TwigBundle:Exception:error.html.twig' (overridden by my custom parent template).

Is this correct? What happens if another kind of error or exception is thrown?


回答1:


Yes it is correct.

All other kind of exceptions will be caught by Kernel and error500.html.twig page will be rendered.

To test it, you can turn off your debug for a moment, by switching second parameter passed to AppKerner constructor in app_dev.php

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

Then you can

  1. type wrong address to test 404 error page
  2. throw any exception to test 500 error page

throw new \Exception(); // test 500 error page

  1. throw

throw new \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException();
or
throw new \Symfony\Component\HttpKernel\Exception\HttpException(403); //to test 403 error page



来源:https://stackoverflow.com/questions/11640372/which-templates-should-be-overridden-in-symfony-2-error-customization

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!