如何在Symfony2中创建类似于Zend preDispatch方法的东西 [英] How to create a something like Zend preDispatch method in Symfony2

查看:69
本文介绍了如何在Symfony2中创建类似于Zend preDispatch方法的东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Symfony 2创建一个项目,我需要在Zend中使用preDispatch之类的方法,该方法将在Controller中的任何操作之前被调用。我是Symfony的新手,在网上冲浪时发现,旧版本的Symfony中有一个preExecute方法,但现在已经不存在了。我可以用什么来解决我的问题?

I'm making a project using a Symfony 2 and I need to have method like preDispatch in Zend which will be called before any action in the Controller. I'm very new in Symfony and a little bit surfing the net I've found that there was a preExecute method in older versions of Symfony but now it's gone. What can I use to solve my problem?

预先感谢。

推荐答案

Symfony 1的preExecute()方法使此操作非常容易,但是就像您说的那样,它在Symfony2中不可用。幸运的是,在Symfony2中,您可以访问事件。该框架在您应用程序的生命周期内调度大量事件。以下是发送的事件的一些列表- http: //symfony.com/doc/2.0/book/internals.html#events

Symfony 1's preExecute() method made this very easy, but like you said, it's not available in Symfony2. Luckily, in Symfony2 you have access to events. The framework dispatches numerous events during your application's lifecycle. Here's a list of some of the events dispatched - http://symfony.com/doc/2.0/book/internals.html#events.

您还可以使用Web调试工具栏查看正在调度的事件以及正在侦听该事件的类(EventListeners)。如果您不熟悉整个事件系统,则上面的链接还将详细介绍整个事件系统。

You can also use the web debug toolbar to see what events are being dispatched, and the classes (EventListeners) that are listening to the event. That above link also goes into detail about the whole event system in case you're not familiar with it.

不幸的是,我不必自己模仿preExecute功能,因此我不知道您需要听什么事件,但是我可以通过阅读来确定文档,您将确切了解您的需求。

Unfortunately I haven't had to mimic preExecute functionality myself, so I don't know exactly what event you would need to listen to, but I'm sure by reading the docs you'll figure out exactly what you need.

希望这会有所帮助。

这篇关于如何在Symfony2中创建类似于Zend preDispatch方法的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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