title: Symfony2 event listener and getting access to Kernel, Request and Response?
body: <p>I'm really struggling to understand this and now I'm just going round in circles.</p>
  
  <p>I've read as much of the manual as possible, paid for a video tutorial, scoured Google and YouTube and just can't get this working.</p>
  
  <p>I am simply trying to set up a listener that activates before every request. I can do this, but my problem is getting access to the various other parts I need.</p>
  
  <p>Below is an example but I think only actual code will help me understand this now.</p>
  
  <p>I would appreciate if anyone could fill in the blanks. It's just an example, but each part will explain to me what it is I need to know.</p>
  
  <p>In config.yml:</p>
  
  <pre><code>services:
      kernel.listener.request_listener:
          class: Acme\Bundle\NewBundle\EventListener\RequestListener
          tags:
              - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
          arguments: [ '@service_container' ]
  </code></pre>
  
  <p>The class:</p>
  
  <pre><code>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-&gt;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-&gt;query-&gt;get('option') ) {
  
          case 1:
              return
          case 2:
  
              $this-&gt;setResponse("hello, message here");
  
              break;
          case 3:
              // *** not sure if this is the way to do it ***
              $response-&gt;headers-&gt;setCookie(new Cookie("test", 1));
              break;
  
      }
  
  }
  
  }
  </code></pre>