Business
Technology
- Administration
- Android
- Apache Solr
- API
- Development Principles
- Django
- Eclipse
- Hibernate
- Javascript/jQuery
- Microsoft .NET
- Microsoft Office
- Patterns
- PHP
- Social Media
- Spring 3.0
- Spring Roo
- Symfony
- Symfony: Doctrine
- Symfony: View
- Symfony: Web Services
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
Linus Torvalds
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 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:
- To add a custom security layer - this helps remove bulky security related code from business and application logic,
- To add objects to the global environment allowing them to be present durring the main logic, or
- To change the output in someway.
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.
- 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,
- 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.
- 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
- Creating a class that extends from sfFilter
- Implement the execute method
- 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
<?php
// $site/apps/frontend/lib/MyPreFilter.class.php
class MyPreFilter extends sfFilter{
public function execute($filterChain){
$this->context->set('Name', "Adam");
$filterChain->execute($filterChain);
}
}
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 MyCustomFilter: 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: ~ MyCustomFilter: class: MyPreFilter cache: ~ execution: ~
The execution plan will be like this:

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.
Add a comment

Lawrence Krubner commented:
I'm implementing a redirect system on our own website at winespectator.com. 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,
Filippo
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?
Thanks