2013-03-18 50 views
14

我真的很努力去理解這一點,現在我只是圍着圈子走。Symfony2事件監聽器並獲取對內核,請求和響應的訪問權限?

我已閱讀儘可能多的手冊,支付視頻教程,搜索谷歌和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; 

    } 

} 

} 

回答

39

中有一些錯誤的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; 
     } 
    } 
} 
17

看來你並不真正瞭解Symfony2的DIC的作品。 您的kernel.request聽衆只能處理案件1和2.對於案例3,您應該使用kernel.response事件。

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)); 
     } 
    } 
} 
+1

一個很好的答案:

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 } 

類。謝謝 – user2143356 2013-03-18 10:11:27