Symfony2 - 处理内核异常侦听器的请求/响应 [英] Symfony2 - Manipulate request/response from the Kernel Exception Listener

查看:190
本文介绍了Symfony2 - 处理内核异常侦听器的请求/响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我已经超过了 error404.html .twig 查看( app / Resources / TwigBundle / views / Exception / )。



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




  • 加载另一个错误视图只有当路线以前缀开始时: / admin /

      $ route = $ event-> getRequest-> get('_ route') - > render()
    //返回NULL


  • 调用 $ event-> container-> get('templating') - > render()功能




当脚本失败时,我最终会出现无限循环(空白页)。



我唯一的工作是:




  • 检索异常代码:

      $ exception = $ event-> getException(); 
    $ code = $ exception-> getCode();


  • 创建新的回复:

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




有关如何实现这一点的任何建议? / p>



课程:

 命名空间Cmt\AdminBundle\EventListener; 

使用Symfony\Component\DependencyInjection\ContainerInterface;
使用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)
{
//获取异常
$ exception = $ event-> getException();

//获取路径
$ path = $ event-> getRequest() - > getPathInfo();

/ *
*仅对路径前缀/ admin /
* /
$
$重新定向回应新的404错误视图
*

而services.yml:

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


解决方案

由于某种原因,这样做有效:

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

//获取路径
$ path = $ event-> getRequest() - > getPathInfo();

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

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

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

$ event-> setResponse($ response);
}

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



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


I am building an administration panel for a website and I would like to change the view called when a 404 exception occurs but only for the admin application. (path: /admin/*)

I have already overdid the error404.html.twig view (at app/Resources/TwigBundle/views/Exception/) for the website.

I thought of the kernel.exception event listener but now I am stuck with two things:

  • Loading another error view only when the route starts with the prefix: /admin/

    $route = $event->getRequest->get('_route')->render()
    //returns NULL
    

  • Calling the $event->container->get('templating')->render() function.

I end up with an infinite loop (blank page) as the script fails.

The only things I've got working are:

  • Retrieving the exception code:

    $exception = $event->getException();
    $code = $exception->getCode();
    

  • Creating a new response:

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

Any suggestions on how to achieve this?

[EDIT]

The class:

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/ 
            */
        }
}

And the 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 }

解决方案

For some reason, this worked:

// 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);
}

Which is basically what I was doing earlier with a different syntax...

@dmirkitanov Anyway, thanks for your help !

这篇关于Symfony2 - 处理内核异常侦听器的请求/响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆