Business

Technology

 

 

 

 

 

The fact that I have entered into IT-related business is proof that businesses have to evolve and keep with time. One has to re-invent continuously.

Kerry Packer

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 !

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="https://www.google.com.pk/">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 http://www.topbritishessays.com/ 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.

www.torontowebdesign.company

Juana commented:

<?a href="http://examples.yourdictionary.com/essay-examples.html">http://examples.yourdictionary.com/essay-examples.html</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

Monty commented:

Hi there would you mind letting me know which hosting company you're working with? I've loaded your blog in 3 completely different browsers and I must say this blog loads a lot quicker then most. Can you suggest a good internet hosting provider at a honest price? Cheers, I appreciate it!

language of desire pdf

 

Monty commented:

Hi there would you mind letting me know which hosting company you're working with? I've loaded your blog in 3 completely different browsers and I must say this blog loads a lot quicker then most. Can you suggest a good internet hosting provider at a honest price? Cheers, I appreciate it!

language of desire pdf

 

Monty commented:

Hi there would you mind letting me know which hosting company you're working with? I've loaded your blog in 3 completely different browsers and I must say this blog loads a lot quicker then most. Can you suggest a good internet hosting provider at a honest price? Cheers, I appreciate it!

Primary Maths Tutoring in Sydney commented:

The learning lab is providing the best Maths and English tutor in Sydney and if you are looking for maths tutor for your child contact us now! for more visit on www.mathslearningcentres.sitew.org

pioneerseo commented:

This is the kind of manual that needs to be given and not the random misinformation that’s at the other blogs. Thanks for sharing.Thanks so much with this fantastic new web site. brustkrebs

Add a comment

*
*
Your email will not be published
*
*