How to create a something like Zend preDispatch method in Symfony2

前端 未结 2 1854
南笙
南笙 2020-12-28 19:04

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

2条回答
  •  灰色年华
    2020-12-28 19:21

    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.

    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.

    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.

    Hope this helps.

提交回复
热议问题