Symfony2事件监听器和访问内核,请求和响应?

时间:2013-03-18 06:56:08

标签: symfony

我真的很难理解这一点,现在我只是围成一圈。

我已经阅读了尽可能多的手册,为视频教程付费,搜索了谷歌和YouTube,但却无法实现这一目标。

我只是想设置一个在每个请求之前激活的监听器。我可以做到这一点,但我的问题是可以访问我需要的各种其他部分。

以下是一个示例,但我认为只有实际代码才能帮助我理解这一点。

如果有人能填补空白,我将不胜感激。这只是一个例子,但每个部分都会向我解释我需要知道的是什么。

在config.yml中:

services:
    kernel.listener.request_listener:
        class: Acme\Bundle\NewBundle\EventListener\RequestListener
        tags:
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
        arguments: [ '@service_container' ]

班级:

namespace Acme\Bundle\NewBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
***do I need to 'use' any others here?***

class RequestListener
{

public function onKernelRequest($container) {

    //reference to these: http://api.symfony.com/2.1/Symfony/Component/HttpKernel/Event/KernelEvent.html
    $kernel =

    //reference to the Request object
    $request = $kernel->getRequest();

    //reference to the Response object
    $response =

    //options:
    //  (1)   continue to run usual content
    //  (2)   stop execution and output a message
    //  (3)   set cookie and continue to run usual content
    switch( $request->query->get('option') ) {

        case 1:
            return
        case 2:

            $this->setResponse("hello, message here");

            break;
        case 3:
            // *** not sure if this is the way to do it ***
            $response->headers->setCookie(new Cookie("test", 1));
            break;

    }

}

}

2 个答案:

答案 0 :(得分:43)

services.yml中的错误很少 为了使您的代码有效,这应该看起来像

services.yml

services:
  listener.requestresponse:
    class: My\AwesomeBundle\Listener\MyListener
    arguments: ['@service_container']
    tags:
      - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
      - { name: kernel.event_listener, event: kernel.response, method: onKernelResponse }

我\ AwesomeBundle \监听\ MyListener.php

namespace My\AwesomeBundle\Listener;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\DependencyInjection\ContainerInterface;

class MyListener
{
    protected $container;

    public function __construct(ContainerInterface $container) // this is @service_container
    {
        $this->container = $container;
    }

    public function onKernelRequest(GetResponseEvent $event)
    {
        $kernel    = $event->getKernel();
        $request   = $event->getRequest();
        $container = $this->container;
    }

    public function onKernelResponse(FilterResponseEvent $event)
    {
        $response  = $event->getResponse();
        $request   = $event->getRequest();
        $kernel    = $event->getKernel();
        $container = $this->container;

        switch ($request->query->get('option')) {
            case 2:
                $response->setContent('Blah');
                break;

            case 3:
                $response->headers->setCookie(new Cookie('test', 1));
                break;
        }
    }
}

答案 1 :(得分:18)

您似乎并未理解symfony2 dic的工作原理。 您的kernel.request侦听器只能处理案例1和2.对于案例3,您应该使用kernel.response事件。

services:
   listener.requestresponse:
     class: Acme\Bundle\NewBundle\EventListener\RequestListener
     tags:
       - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
       - { name: kernel.event_listener, event: kernel.response, method: onKernelResponse }

班级:

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpFoundation\Response;

class MyListener
{
   public function onKernelRequest(GetResponseEvent $event)
   {
       $request = $event->getRequest();
       if ($request->query->get('option') == 2) {
           $event->setResponse(new Response("hello, message here"));
       }
   }

   public function onKernelResponse(FilterResponseEvent $event)
   {
       $response = $event->getResponse();
       $request  = $event->getRequest();

       if ($request->query->get('option') == 3) {
           $response->headers->setCookie(new Cookie("test", 1));
       }
    }
}