Categories
newbie symfony

One more time about symfony filters

Symfony filters is powerful mechanism to perform system wide authentications, database actions, etc.
One of the ways you may want to use it is system wide preExecute action. The logic of preExecute requires it to be added per module but often you may need to perform same bunch of actions for all modules. So why don’t group them and create init filter.

apps/frontend/lib/initFilter.class.php

class initFilter 
{
  public function execute($filterChain) 
  {
    // code you want to execute before every action
    $filterChain->execute();
  }
}

Please note that line $filterChain->execute(); is essential as it notifies system to keep running other filters.
Next step you add this new filter to

apps/frontend/config/filters.yml

you have to make sure that it’s added in correct location:

rendering: ~
security:  ~
 
# insert your own filters here
 
init:
  class: initFilter
 
cache:     ~
execution: ~

As if you will add it e.g. before rendering there won’t be access to database layer, etc.
If you want to add own filter for database layer – you’ll have to add it after rendering but before security as the most probably your security layer will use database.
So that’s it – easy and powerful.