Symfony自定义错误页面通过重写ExceptionController

时间:2014-11-24 16:27:36

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

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

无论原因(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);
    }

}

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

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

1 个答案:

答案 0 :(得分:2)

在开始时,您需要在控制器中创建操作:

<?php
namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class ErrorController extends Controller
{

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

然后你需要创建一个监听器:

<?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,他的购物车以及与会话相关的其他事项。