The lifeblood of our business is that R&D spend. There's nothing that flows through a pipe or down a wire or anything else. We have to continuously create new innovation that lets people do something they didn't think they could do the day before.

Steve Ballmer

Symfony: Execution Filters

Looking for a Symfony Developer?

If you or your company are looking for a Symfony Developer contact us or read about our Symfony Development Service

Symfony: Execution Filters key visual

Symfony is a powerful PHP Framework. One of the features that I have really taken advantage in my last project is request filtering or execution filtering. Request filters allow you to inspect the request before and after the main logic is executed. Why would one want to do this? There are many reasons to implement request filtering:

The above reasons are great but unless you understand them it just a "nice to have feature".

On my last project we needed to use all three above mentioned reasons and in this article we will explore two of them. Adding objects to the global environment and changing the output. This article is split up into 3 parts.

  1. Symfony: Execution Filters - This the introduction, here I show you what is needed to set up the filter and an overview as to whats going on,
  2. Symfony: Execution Filters: Add objects to global environment - Here we look at using a filter to add objects into the global environment before the main logic is run.
  3. Symfony: Execution Filters: Controlling output - Here we look at using a filter to modify the output of an action before sending the request off to the view.

Creating a pre execution filter

There are 3 steps in setting up request filter

  1. Creating a class that extends from sfFilter
  2. Implement the execute method
  3. Register the filter with Symfony.

Extending sfFilter

In the application's lib directory "$site/apps/<Application>/lib" create a file called MyPreFilter.class.php.

Open this new file and add the following code


// $site/apps/frontend/lib/MyPreFilter.class.php
class MyPreFilter extends sfFilter{

  public function execute($filterChain){

      $this->context->set('Name', "Adam");




This is a pre filter because its logic is executed before the call to $filterChain->execute($filterChain). To implement a post filter simply execute your logic after the call to $filterChain->execute($filterChain);

Note: You must call the execute() method on the filter chain. This will allow symfony to execute the remaining filters like Execution filter, a pre-filter that calls your main logic, and the rendering filter, a post-filter that renders your page.

Register your filter with Symfony

The next step is to register your filter with Symfony. This is simply a matter of opening the application's filters.yml file and adding the following entry

# $site/apps/frontend/config/filters.yml

  class: MyPreFilter

Note the order of the filters in the filters.yml file is very important. Symfony builds a "Filter chain" in the order that they are defined in the filters.yml file.  It then kicks off the execution by calling the first filter, the first filter, be it pre or post, then calles the next filter, and so on.

Symfony Request Filter Execution Plan

In a symfony filters.yml file that lookes like this

rendering: ~
security:  ~

  class: MyPreFilter        

cache:     ~
execution: ~

The execution plan will be like this:

Symfony Filter Execution Plan

Each registered filter gets two opportunities to execute its code. Once before the main application logic and once after.

Read on: Symfony: Execution Filters: Add objects to global environment

If you found this article useful, please be sure to check out the related articles below.

Lawrence Krubner commented:

I'm implementing a redirect system on our own website at This post was very useful. 

Filippo commented:

Sigh, it seems it doesn't work at all!

Which version of Symfony is used? I'm using 1.4!

Thank you,



Adam Pullen commented:

If you could provide some more information, i.e.

* what your trying to do, and

* what "is not working" then may be I could be of

some more help.

1) Did you register your filter in filters.yml?

2) Did you clear the cache?

3) Try using the frontend_dev.php controler and view the logs. Did your filter get executed?


Mika commented:

Excellent stuff ! It worked like a charm.

I am actually building a system to log calls and responses of a REST api. This seems to be the way to go !

vally commented:

 This will offer a genuine feel to your traveling encounter and allow your mind to encounter the excitement of becoming a member of the military to fly your preferred type of chopper. <a href="">Google</a>


asdf commented:

dsafsadf asdf sadf sadfasd fsdf

adds commented:

dsafsadf asdf sadf sadfasd fsdf

Patterson commented:

It is my desire to begin a website for top examination paper writing service. I have been written more assignment papers for a long time and I believe these services are helpful for my education. Thank you very much to your blog writing services and other content writing tips.

lafseo commented:

Symfony is gone for building hearty applications in a venture setting, and means to give engineers full control over the setup from the index structure to the remote libraries, practically everything can be modified.

Juana commented:

<?a href=""></a>?

fdasa commented:

Since the Orthodontist Salary is so high, many individuals that are interested in following a healthcare career would like to know more about this type of wage

Add a comment

Your email will not be published