11

如何查看 Symfony2 中的所有可用事件?

我在谷歌上找到了一个命令

php app\console container:debug --show-private

但它没有显示所有可用的事件。类似名为“security.interactive_login”的事件未在其中列出。有没有办法查看可用的事件?

4

2 回答 2

32

Console command

You can run:

app/console debug:event-dispatcher

This will show you a detailed summary of every subscriber, in order of priority per event. Unfortunately this won't show you all possible events, since it's infeasible to query the container for any events that could be registered due to the inherently dynamic nature of the events system.

To understand events you'll need to refer to docs and code of each component and bundle.

Documentation is the best place to start

Symfony standard ships with a multitude of events. Each Symfony component and bundle may or may not define events — your best bet is to look at each component or bundle's documentation for references to events.

Some very common events can be found in the docs:

Code analysis

I used PhpStorm to look for all subclasses of Symfony's base Event class (Symfony\Component\EventDispatcher\Event).

I generated an inheritance tree each child is a subclass of it's parent.

* note: prepend Symfony\Component\ to find the FQN

I make no claim that these are all public events you can/should hook into — this is just one way to programmatic examine 3rd party code and get a sense for potential idioms.

For instance I noticed that both the HttpKernel, Security, and Console components use namespaced constants to expose their keys, see:

于 2015-01-09T07:30:45.640 回答
2

container:debug命令显示注册到依赖注入容器的所有服务。使用参数show-private它还将显示带有public=false标记的服务。

因此,由于大多数事件可能不是服务,因此您使用的命令不会为您提供可用事件的列表。但是为了让您有可能搜索可用事件,您可以尝试以下命令:

php app/console container:debug --show-private | grep -i "listener"

由于大多数事件处理程序的定义中可能包含“侦听器”一词,因此您会发现其中很多。如果您想获得有关这些侦听器处理的事件的更详细信息,只需调用命令并指定服务 ID。例如,如果您正在使用 FOSUserBundle,这将为您提供交互式登录侦听器的描述:

php app/console container:debug fos_user.security.interactive_login_listener
于 2013-11-03T00:22:43.923 回答