Business

Technology

 

 

 

 

 

To turn really interesting ideas and fledgling technologies into a company that can continue to innovate for years, it requires a lot of disciplines.

Steve Jobs

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

<?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:

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 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

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 !

Add a comment

*
*
Your email will not be published
*
*