Symfony2 事件侦听器并获得对内核、请求和响应的访问权限? [英] Symfony2 event listener and getting access to Kernel, Request and Response?

查看:28
本文介绍了Symfony2 事件侦听器并获得对内核、请求和响应的访问权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我已经阅读了尽可能多的手册,支付了视频教程的费用,在 Google 和 YouTube 上搜索,但无法使其正常工作.

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.

在 config.yml 中:

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

班级:

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;

    }

}

}

推荐答案

services.yml
为了使您的代码工作,这应该看起来像

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

这篇关于Symfony2 事件侦听器并获得对内核、请求和响应的访问权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆