Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As @Arms suggested me, I've started exploring the event mechanism of Symfony 2. So now I'm gonna write down the code, that solved my problem (all configurations I'm doing in YAML, but you can do it in XML or PHP as well).</p> <p>At first you need to describe in Symfony configuration what kind of event would you like to listen and what the kernel should call when the event occurs. For this open the configuration file <strong>app\config\config.yml</strong> and add the following code:</p> <pre><code>services: younamespace.yourbundle.listener.preexecute: class: Location\Of\Your\Listener\Class tags: - { name: kernel.event_listener, event: kernel.controller, method: onKernelController } </code></pre> <p>Your class should implement the onKernelController method in the following way:</p> <pre><code>public function onKernelController(FilterControllerEvent $event) { if(HttpKernelInterface::MASTER_REQUEST === $event-&gt;getRequestType()) { $controllers = $event-&gt;getController(); if(is_array($controllers)) { $controller = $controllers[0]; if(is_object($controller) &amp;&amp; method_exists($controller, 'preExecute')) { $controller-&gt;preExecute(); } } } } </code></pre> <p>After this every time when you create a preExecute method in your controller, it will be called before executing any actions, so you can do lot of stuff in that method, like initializing variables, or some checks before writing to DB and so on.</p> <p>You can also look at configuration in details <a href="http://symfony.com/doc/2.0/cookbook/request/mime_type.html">here</a> (this example is for kernel.request event, but it can help you to understand)</p> <p>Have fun ;)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload