Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 event listener and getting access to Kernel, Request and Response?

Tags:

symfony

I'm really struggling to understand this and now I'm just going round in circles.

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.

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.

Below is an example but I think only actual code will help me understand this now.

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.

In 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' ]

The class:

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;

    }

}

}
like image 335
user2143356 Avatar asked Mar 18 '13 06:03

user2143356


2 Answers

There are few mistakes in your services.yml
In order to make your code work, this should look like

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;
        }
    }
}
like image 137
Touki Avatar answered Oct 22 '22 01:10

Touki


It seems that you don't actually understand how symfony2 dic works. Your kernel.request listener can handle only cases 1 and 2. For case 3 you should use kernel.response event.

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 }

The class:

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));
       }
    }
}
like image 20
Ziumin Avatar answered Oct 22 '22 03:10

Ziumin