首页 > 解决方案 > 无法覆盖 symfony 5 异常模板

问题描述

我按照 Symfony 当前文档覆盖异常模板: 如何自定义错误页面

在我的 AccesDeniedListener.php 类中,我放了:

 public function onKernelException(ExceptionEvent $event, string $eventName = null): void
{
    $exception = $event->getThrowable();
    if (!$exception instanceof AccessDeniedException) {
        return;
    }

    // ... perform some action (e.g. logging)

    // optionally set the custom response
    $event->setResponse(new Response(null, 403));

    // or stop propagation (prevents the next exception listeners from being called)
    //$event->stopPropagation();
}

我已经安装了 symfony/twig-pack

在我的模板文件夹中,我添加了 Execption twigs :

在此处输入图像描述

最后我的新树枝没有被应用

标签: symfonytwig

解决方案


如果您的应用程序环境设置为 prod,您应该获得自定义错误页面。如果您的应用程序环境设置为 dev,那么您仍然会收到开发错误消息。要在 dev 中测试您的自定义页面,您可以按照您遵循的指南中的说明添加特殊路线。

# config/routes/dev/framework.yaml
_errors:
    resource: '@FrameworkBundle/Resources/config/routing/errors.xml'
    prefix:   /_error

使用 dev 中的配置,您应该能够访问http://example.com/_error/403


推荐阅读