Symfony2 - 处理来自内核异常侦听器的请求/响应

时间:2011-09-19 01:50:48

标签: exception symfony event-listener

我正在为网站构建一个管理面板,我想更改管理应用程序发生404异常但仅时调用的视图。 (path: /admin/*)

我已经过度使用该网站的error404.html.twig视图(app/Resources/TwigBundle/views/Exception/)。

我想到了kernel.exception事件监听器,但现在我遇到了两件事:

  • 仅当路线以前缀/admin/

    开头时才加载另一个错误视图
    $route = $event->getRequest->get('_route')->render()
    //returns NULL
    
  • 调用$event->container->get('templating')->render()函数。

当脚本失败时,我最终得到一个无限循环(空白页面)。

我唯一能做的就是:

  • 检索异常代码:

    $exception = $event->getException();
    $code = $exception->getCode();
    
  • 创建新回复:

    $response = new Response();
    $event->setResponse($response);
    

有关如何实现这一目标的任何建议吗?

[编辑]

班级:

namespace Cmt\AdminBundle\EventListener;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Bundle\TwigBundle\TwigEngine;

class AdminActionListener
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @var TwigEngine
     */
    protected $templating;


    /**
     * @param ContainerInterface $container
     */
    public function __construct(ContainerInterface $container, TwigEngine $templating){
        // assign value(s)
        $this->container = $container;
        $this->templating = $templating;
    }

    /**
     * 
     * @param GetResponseForExceptionEvent $event
     */
    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        // get exception
        $exception = $event->getException();

        // get path
        $path = $event->getRequest()->getPathInfo();

        /*
        * Redirect response to new 404 error view only
        * on path prefix /admin/ 
            */
        }
}

和services.yml:

services:
    cmt_admin.exception.action_listener:
        class: Cmt\AdminBundle\EventListener\AdminActionListener
        arguments: [@service_container] [@templating]
        tags:
            -   { name: kernel.event_listener, event: kernel.exception, method: onKernelException }

2 个答案:

答案 0 :(得分:3)

你可以尝试这个:

public function __construct(TwigEngine $templating)
{
    $this->templating = $templating;
}

public function onKernelException(GetResponseForExceptionEvent $event)
{
    static $handling;

    $exception = $event->getException();

    if (true === $handling) {
        return;
    }
    $handling = true;

    $code = $exception->getCode();

    if (0 !== strpos($event->getRequest()->getPathInfo(), '/admin') && 404 === $code) {
        $message = $this->templating->render('AcmeBundle:Default:error404new.html.twig', array());
        $response = new Response($message, $code);
        $event->setResponse($response);
    }

    $handling = false;
}

$ templating变量可以在services.xml中传递:

<service id="acme.exception.listener" class="%acme.exception.listener.class%">
    <tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" />
    <argument type="service" id="templating" />
</service>

答案 1 :(得分:3)

出于某种原因,这有效:

// get exception
$exception = $event->getException();

// get path
$path = $event->getRequest()->getPathInfo();

if ($exception->getStatusCode() == 404 && strpos($path, '/admin') === 0){

    $templating = $this->container->get('templating');

    $response = new Response($templating->render('CmtAdminBundle:Exception:error404.html.twig', array(
        'exception' => $exception
    )));

    $event->setResponse($response);
}

这基本上是我之前用不同的语法做的...

@dmirkitanov无论如何,谢谢你的帮助!