gpt4 book ai didi

symfony - 如何在symfony 3.1中自定义错误页面模板?

转载 作者:行者123 更新时间:2023-12-03 07:53:40 26 4
gpt4 key购买 nike

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

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

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

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”的 bundle 软件,并创建了名称为 “TwigBundle \ Controller \ ExceptionController.php”的 Controller 文件夹和类

Config.yml

Twig 配置
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中运行。
但是我仍然没有得到自定义错误模板。

最佳答案

在第一个中,您需要在以下位置创建新的错误模板:/YOUR_PROJECT_FOLDER/app/Resources/TwigBundle/views/Exception/
例如,创建一个: error.html.twig 处理所有错误

在第二个中,您必须为错误 View 编写一些模板内容。
为了进行实验,请将这些内容放入我们的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(它可以已经存在)
# app/config/routing_dev.yml
_errors:
resource: "@TwigBundle/Resources/config/routing/errors.xml"
prefix: /_error

添加此路由后,让我们检查新的错误模板。在网络浏览器中转到此链接:
/app_dev.php/_error/418其中 418只是错误状态代码

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

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

关于symfony - 如何在symfony 3.1中自定义错误页面模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40257640/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com