【问题标题】:Symfony Custom Error Page By Overriding ExceptionControllerSymfony 通过覆盖 ExceptionController 自定义错误页面
【发布时间】:2014-11-24 16:27:36
【问题描述】:

我想要做的是自定义错误页面,它们不仅会扩展基本布局,而且我还希望在这些页面中额外销售内容,因此仅更改模板不是一种选择

不管是什么原因(404 Not Found 或只是缺少变量)我都想显示我的模板和我的内容

我花了几个小时试图让这件事顺利进行

app/console --version
Symfony version 2.5.6 - app/dev/debug

我尝试了一些资源,但无法正常工作。列举几个:

http://symfony.com/doc/current/reference/configuration/twig.html http://symfony.com/doc/current/cookbook/controller/error_pages.html

我在没有调试的情况下在 dev 中运行,请参阅下面的 app_dev.php

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

按照教程我得到了这些额外的位

app/config/config.yml

twig:
    exception_controller:  SomethingAppBundle:Exception:show

在我的包中

<?php

namespace Something\AppBundle\Controller;

use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
use Symfony\Component\HttpKernel\Exception\FlattenException;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class ExceptionController extends Controller
{
    public function showAction( FlattenException $error, DebugLoggerInterface $debug)
    {
        print_r($error);
    }

}

但是我的错误控制器没有被执行,

我故意通过尝试在不同的控制器中回显未定义的变量来导致错误,因为它应该处理来自整个应用程序的错误

【问题讨论】:

  • “我尝试了一些无用的资源,实际上我尝试了更多,但出于沮丧我只提这两个,毕竟它的“文档”是正确的,尽管没有任何效果从那里开始,讨厌吹牛,但 WTFPOS 文档”。我知道你很沮丧,但请不要责怪别人。如果这是您的诚实意见,请向我展示不适合您的代码示例,我们将对其进行修复。就像我们在过去 3 年中对所有其他文档错误所做的那样
  • 抱歉,上面的代码应该适用于“替换默认异常控制器”部分
  • @tomhre 这个问题你解决了吗?

标签: symfony exception-handling error-handling custom-error-pages


【解决方案1】:

一开始你需要在控制器中创建动作:

<?php
namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class ErrorController extends Controller
{
    
    public function notFoundAction()
    {
        return $this->render('error/404.html.twig');
    }
}

那么你需要创建一个 Listener:

<?php
namespace AppBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class NotFoundHttpExceptionListener
{
    private $controller_resolver;
    private $request_stack;
    private $http_kernel;

    public function __construct($controller_resolver, $request_stack, $http_kernel)
    {
        $this->controller_resolver = $controller_resolver;
        $this->request_stack = $request_stack;
        $this->http_kernel = $http_kernel;
    }
    public function onKernelException(GetResponseForExceptionEvent $event)
    {

        if ($event->getException() instanceof NotFoundHttpException) {

            $request = new \Symfony\Component\HttpFoundation\Request();
            $request->attributes->set('_controller', 'AppBundle:Error:notFound');
            $controller = $this->controller_resolver->getController($request);

            $path['_controller'] = $controller;
            $subRequest = $this->request_stack->getCurrentRequest()->duplicate(array(), null, $path);

            $event->setResponse($this->http_kernel->handle($subRequest, HttpKernelInterface::MASTER_REQUEST)); // Simulating "forward" in order to preserve the "Not Found URL"

        }
    }
}

现在注册服务:

#AppBundle/Resources/config/services.yml
services:
    kernel.listener.notFoundHttpException:
            class: AppBundle\EventListener\NotFoundHttpExceptionListener
            tags:
                - { name: kernel.event_listener, event: kernel.exception, method: onKernelException, priority: -10 }
            arguments: [ @controller_resolver, @request_stack, @http_kernel ]

没有对此进行测试,但它应该可以工作;)

编辑:

经过测试,有效。在呈现的页面上,您有一个会话,因此您可以访问 app.user、他的购物车以及与会话相关的其他事项。

【讨论】:

  • 嗨,Pawel,上面的代码正在运行,我已经对其进行了更改以处理其他未发现的异常,谢谢。但这不是我寻找的解决方案,对不起,赞成但恐怕不会接受。我想通过“替换默认的异常控制器”来做到这一点,因为我希望我可以避免子请求和额外的服务
  • 嗨@tomhre!我认为:如果您想在呈现的页面 404 上进行会话,则无法通过覆盖 ExceptionController。 Symfony 路由组件在安全组件之前被调用。如果路由器没有找到路径,则会在没有用户数据和额外销售内容的情况下引发错误。在上述解决方案中,我发送了一个附加请求,该请求被标记为主请求。
  • "替换默认的 ExceptionController" 如果您需要更多的灵活性,而不仅仅是覆盖模板,那么您可以更改呈现错误页面的控制器。例如,您可能需要将一些额外的变量传递到您的模板中。 - 对我来说听起来我可以在那里有额外的内容,我遇到的问题是,如果我将它更改为 Homepage:index 但只要它是 Error:index 就会发疯,树枝配置会在错误时将我重定向到主页跨度>
  • 最重要的是,您的解决方案不关注环境,因此在开发模式下我失去了所有调试
  • 在开发模式分析器在渲染页面上工作正常。您在“幽灵”标签中仍然有异常:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多