【问题标题】:can't override symfony 5 exception templates无法覆盖 symfony 5 异常模板
【发布时间】:2020-11-15 00:45:01
【问题描述】:

我按照 Symfony 当前文档来覆盖异常模板: How to Customize Error Pages

在我的 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 :

我的新树枝终于没有应用

【问题讨论】:

  • 你确定你处于 prod 模式(APP_ENV=prod) 吗?另外请编辑您的帖子并添加error403.html.twig的代码

标签: symfony twig


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2013-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-16
    • 1970-01-01
    • 2014-08-30
    相关资源
    最近更新 更多