MVC Framework 是离不开 EventListener 的. 简单的了解一下 Symfony 4 的事件监听机制. 非常的简单易用, 强大.
先来看一下 symfony/website-skeleton
默认的事件监听清单. 在终端里打印出来看一下:
$ bin/console debug:event-dispatcher
Console 会输出一堆 Listener, 不过编排的非常的美丽.
Registered Listeners Grouped by Event
=====================================
"console.command" event
-----------------------
------- ---------------------------------------------------------------------- ----------
Order Callable Priority
------- ---------------------------------------------------------------------- ----------
#1 Symfony\Component\HttpKernel\EventListener\DumpListener::configure() 1024
#2 Symfony\Bridge\Monolog\Handler\ConsoleHandler::onCommand() 255
------- ---------------------------------------------------------------------- ----------
"console.error" event
---------------------
------- ----------------------------------------------------------------------------------- ----------
Order Callable Priority
------- ----------------------------------------------------------------------------------- ----------
#1 Symfony\Bundle\SwiftmailerBundle\EventListener\EmailSenderListener::onException() 0
#2 Symfony\Bundle\MakerBundle\Event\ConsoleErrorSubscriber::onConsoleError() 0
#3 Symfony\Component\Console\EventListener\ErrorListener::onConsoleError() -128
------- ----------------------------------------------------------------------------------- ----------
"console.terminate" event
-------------------------
------- ----------------------------------------------------------------------------------- ----------
Order Callable Priority
------- ----------------------------------------------------------------------------------- ----------
#1 Symfony\Bundle\SwiftmailerBundle\EventListener\EmailSenderListener::onTerminate() 0
#2 Symfony\Bundle\MakerBundle\Event\ConsoleErrorSubscriber::onConsoleTerminate() 0
#3 Symfony\Component\Console\EventListener\ErrorListener::onConsoleTerminate() -128
#4 Symfony\Bridge\Monolog\Handler\ConsoleHandler::onTerminate() -255
------- ----------------------------------------------------------------------------------- ----------
"debug.security.authorization.vote" event
-----------------------------------------
------- ------------------------------------------------------------------------- ----------
Order Callable Priority
------- ------------------------------------------------------------------------- ----------
#1 Symfony\Bundle\SecurityBundle\EventListener\VoteListener::onVoterVote() 0
------- ------------------------------------------------------------------------- ----------
"kernel.controller" event
-------------------------
------- ----------------------------------------------------------------------------------------------- ----------
Order Callable Priority
------- ----------------------------------------------------------------------------------------------- ----------
#1 Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector::onKernelController() 0
#2 Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::onKernelController() 0
#3 Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController() 0
#4 Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController() 0
#5 Sensio\Bundle\FrameworkExtraBundle\EventListener\HttpCacheListener::onKernelController() 0
#6 Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController() -128
------- ----------------------------------------------------------------------------------------------- ----------
"kernel.controller_arguments" event
-----------------------------------
------- --------------------------------------------------------------------------------------------------- ----------
Order Callable Priority
------- --------------------------------------------------------------------------------------------------- ----------
#1 Sensio\Bundle\FrameworkExtraBundle\EventListener\SecurityListener::onKernelControllerArguments() 0
#2 Sensio\Bundle\FrameworkExtraBundle\EventListener\IsGrantedListener::onKernelControllerArguments() 0
------- --------------------------------------------------------------------------------------------------- ----------
"kernel.exception" event
------------------------
------- ------------------------------------------------------------------------------------ ----------
Order Callable Priority
------- ------------------------------------------------------------------------------------ ----------
#1 Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelException() 0
#2 Symfony\Bundle\SwiftmailerBundle\EventListener\EmailSenderListener::onException() 0
#3 Symfony\Component\HttpKernel\EventListener\ExceptionListener::logKernelException() 0
#4 Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelException() -64
#5 Symfony\Component\HttpKernel\EventListener\ExceptionListener::onKernelException() -128
#6 Symfony\Component\HttpKernel\EventListener\ExceptionListener::onKernelException() -2048
------- ------------------------------------------------------------------------------------ ----------
"kernel.finish_request" event
-----------------------------
------- ---------------------------------------------------------------------------------------- ----------
Order Callable Priority
------- ---------------------------------------------------------------------------------------- ----------
#1 Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelFinishRequest() 0
#2 Symfony\Component\HttpKernel\EventListener\SessionListener::onFinishRequest() 0
#3 Symfony\Component\HttpKernel\EventListener\TranslatorListener::onKernelFinishRequest() 0
#4 Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelFinishRequest() 0
#5 Symfony\Bundle\SecurityBundle\Debug\TraceableFirewallListener::onKernelFinishRequest() 0
------- ---------------------------------------------------------------------------------------- ----------
"kernel.request" event
----------------------
------- ------------------------------------------------------------------------------------------------- ----------
Order Callable Priority
------- ------------------------------------------------------------------------------------------------- ----------
#1 Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure() 2048
#2 Symfony\Component\HttpKernel\EventListener\ValidateRequestListener::onKernelRequest() 256
#3 Symfony\Component\HttpKernel\EventListener\SessionListener::onKernelRequest() 128
#4 Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest() 32
#5 Symfony\Bundle\FrameworkBundle\EventListener\ResolveControllerNameSubscriber::onKernelRequest() 24
#6 Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest() 16
#7 Symfony\Component\HttpKernel\EventListener\TranslatorListener::onKernelRequest() 10
#8 Symfony\Bundle\SecurityBundle\Debug\TraceableFirewallListener::onKernelRequest() 8
------- ------------------------------------------------------------------------------------------------- ----------
"kernel.response" event
-----------------------
------- -------------------------------------------------------------------------------------------- ----------
Order Callable Priority
------- -------------------------------------------------------------------------------------------- ----------
#1 Symfony\Component\HttpKernel\EventListener\ResponseListener::onKernelResponse() 0
#2 Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::onKernelResponse() 0
#3 Symfony\Component\WebLink\EventListener\AddLinkHeaderListener::onKernelResponse() 0
#4 Sensio\Bundle\FrameworkExtraBundle\EventListener\HttpCacheListener::onKernelResponse() 0
#5 Symfony\Component\Security\Http\RememberMe\ResponseListener::onKernelResponse() 0
#6 Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelResponse() -100
#7 Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener::onKernelResponse() -128
#8 Symfony\Component\HttpKernel\EventListener\SessionListener::onKernelResponse() -1000
#9 Symfony\Component\HttpKernel\EventListener\StreamedResponseListener::onKernelResponse() -1024
------- -------------------------------------------------------------------------------------------- ----------
"kernel.terminate" event
------------------------
------- ----------------------------------------------------------------------------------- ----------
Order Callable Priority
------- ----------------------------------------------------------------------------------- ----------
#1 Symfony\Bundle\SwiftmailerBundle\EventListener\EmailSenderListener::onTerminate() 0
#2 Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelTerminate() -1024
------- ----------------------------------------------------------------------------------- ----------
"kernel.view" event
-------------------
------- ----------------------------------------------------------------------------------- ----------
Order Callable Priority
------- ----------------------------------------------------------------------------------- ----------
#1 Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelView() 0
------- ----------------------------------------------------------------------------------- ----------
默认框架提供的事件组大概是下面的几组:
- console.command
- console.error
- console.terminate
- debug.security.authorization.vote
- kernel.controller
- kernel.controller_arguments
- kernel.exception
- kernel.finish_request
- kernel.request
- kernel.response
- kernel.terminate
- kernel.view
这其中, kernel 事件是需要重点关注的. 接下一个一个的详细来玩弄一下.