Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

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

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: AcmeBundleNewBundleEventListenerRequestListener
        tags:
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
        arguments: [ '@service_container' ]

The class:

namespace AcmeBundleNewBundleEventListener;

use SymfonyComponentHttpKernelEventGetResponseEvent;
***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;

    }

}

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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: MyAwesomeBundleListenerMyListener
    arguments: ['@service_container']
    tags:
      - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
      - { name: kernel.event_listener, event: kernel.response, method: onKernelResponse }

MyAwesomeBundleListenerMyListener.php

namespace MyAwesomeBundleListener;

use SymfonyComponentHttpKernelEventGetResponseEvent;
use SymfonyComponentHttpKernelEventFilterResponseEvent;
use SymfonyComponentHttpFoundationCookie;
use SymfonyComponentDependencyInjectionContainerInterface;

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;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...