15

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

我已经尽可能多地阅读了手册,付费购买了视频教程,搜索了 Google 和 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;

    }

}

}
4

2 回答 2

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 }

My\AwesomeBundle\Listener\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;
        }
    }
}
于 2013-03-18T08:36:29.440 回答
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));
       }
    }
}
于 2013-03-18T08:50:52.230 回答