Symfony2 事件监听器 [英] Symfony2 event listener

查看:29
本文介绍了Symfony2 事件监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我试图找出这些听众,但我在 symfony 网站上找到有关他们的任何信息时遇到问题..

最初,我想创建一个在每个页面加载时触发的侦听器...我认为这可能对整体系统性能有害,所以我想让它只在:/和/otherpage

但同样,我在查找有关从何处开始使用侦听器的任何信息时遇到问题.任何帮助表示赞赏.. 所有这些侦听器都将使用 Doctrine 检查数据库并根据它找到的内容设置会话..

再次感谢任何帮助或建议.谢谢.

解决方案

我做了类似的事情来检查子域没有改变.您可以将侦听器作为服务放入配置文件中,如下所示:

服务:page_load_listener:类:Acme\SecurityBundle\Controller\PageLoadListener论据:安全:@security.context",容器:@service_container"标签:- { 名称:kernel.event_listener,事件:kernel.request,方法:onKernelRequest,优先级:64 }

我不确定优先级是如何工作的,但我发现如果它设置得太高,它就不会在应用程序的其余部分之前运行.我的待办事项清单上有更多研究.

这是一个听众可能看起来的例子.

命名空间 Acme\SecurityBundle\Controller;使用 Symfony\Bundle\FrameworkBundle\Controller\Controller;使用 Symfony\Component\Security\Core\SecurityContext;使用 Symfony\Component\HttpKernel\Event\GetResponseEvent;类 PageLoadListener 扩展控制器{私人 $securityContext;受保护的 $container;受保护的 $query;公共函数 __construct(SecurityContext $context, $container, 数组 $query = array()){$this->securityContext = $context;$this->container = $container;$this->query = $query;}公共函数 onKernelRequest(GetResponseEvent $event){//如果你正在传递任何数据$request = $event->getRequest();//如果你需要更新会话数据$session = $request->getSession();//你还需要做什么...}}

我不确定将其设置为仅在某些页面上运行的最佳方法,但我最好的猜测是检查路线,并且仅在路线与您设置的任何内容匹配时才点击您的数据库.

希望能帮助您入门!

格雷格

So, I'm trying to figure out these listeners but I'm having issues finding any information on the symfony site regarding them..

Initially, I wanted to create a listener that would trigger on every page load... I figured that could be detrimental to the overall system performance so i then thought of having it trigger only on: / and /otherpage

But again, i'm having issues finding any information on where to get started with the listener. Any help appreciated.. All this listener will be doing, is using Doctrine to check the database and setting a session based on what it finds..

Again, any help or suggestions appreciated. Thanks.

解决方案

I do something similar to check the subdomain hasn't changed. You can put the listener in as a service in your config file as follows:

services:
    page_load_listener:
        class: Acme\SecurityBundle\Controller\PageLoadListener
        arguments: 
            security: "@security.context", 
            container: "@service_container"
        tags:
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest, priority: 64 }

I'm not exactly sure how priority works but I've found if it's set too high it wouldn't run before the rest of the application. It's on my to-do list to research a little more.

Here is an example of how the listener could look.

namespace Acme\SecurityBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class PageLoadListener extends controller
{
    private $securityContext;
    protected $container;
    protected $query;

    public function __construct(SecurityContext $context, $container, array $query = array())
    {
        $this->securityContext = $context;
        $this->container = $container;
        $this->query = $query;
    }

    public function onKernelRequest(GetResponseEvent $event)
    {       
        //if you are passing through any data
        $request = $event->getRequest();

        //if you need to update the session data
        $session = $request->getSession();              

        //Whatever else you need to do...

    }
}

I'm not sure of the best way to set it to only run on certain pages, but my best guess would be to check the route and only hit your db it when the route matches whatever you set.

Hope that gets you started!

Greg

这篇关于Symfony2 事件监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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