MVC Framework 是離不開 EventListener 的. 簡單的了解一下 Symfony 4 的事件監(jiān)聽機(jī)制. 非常的簡單易用, 強(qiáng)大.
先來看一下 symfony/website-skeleton
默認(rèn)的事件監(jiān)聽清單. 在終端里打印出來看一下:
$ 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
------- ----------------------------------------------------------------------------------- ----------
默認(rèn)框架提供的事件組大概是下面的幾組:
- 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 事件是需要重點(diǎn)關(guān)注的. 接下一個一個的詳細(xì)來玩弄一下.