Business

Technology

 

 

 

 

 

I think it's fair to say that personal computers have become the most empowering tool we've ever created. They're tools of communication, they're tools of creativity, and they can be shaped by their user.

Bill Gates

Symfony: Execution Filters:Add objects to global environment

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:Add objects to global environment key visual

This is the second in a three part article on Symfony Request filters. Here we look at using a filter to add objects into the global environment before the main logic is run. This article is split up into 3 parts.

  1. Symfony: Execution Filters - 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 - This; 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.

Overview

In the Time Tracker application an API was built to allow Android, iPhone and other applications to use the Platform. The URL was structured in such away that a lot of information could be delivered in a neat and consistant manner. A lot of the functions needed access to the company, and user information. We could have written code inside each action to locate this data or, as we did, we could implement a request filter to place this common information in context for all actions. Read on to see how it is done.

Creating the pre execution filter

This filter will parse the URL and extract the company name that will be present in all URLs coming to our site. Take a look at the URL below

http:// time-tracker. time-tracker.ch /api.php/ api/ 2345fgted6/ esdcttridk/ task/ list

This url is packed full of information. Below is the decoded URL:

  • Company Slug - Uniquly identifies the company within the system
  • Domain Name - The domain name of the application
  • Entry Point - The API Entry point
  • Api Prefix - a special prefix to tell us to invoke the API application
  • Application API Key - The key of the application making the call.
  • User Key - A key that uniquly identifies a user within the system
  • Module - The symfony module
  • Action - The symfony action

We will be creating a filter that puts the Company object into the sfContext using a symfony pre-filter. To do this we will

  1. Create a filter that extends sfFilter,
  2. Parse the URL to get the company slug, 
  3. Read our Company database table, 
  4. Place the company object into sfContext, and finally,
  5. Register the filter

Extending sfFilter

In the application's lib directory "$site/apps/api/lib" create a file called CompanyContextFilter.class.php.

Open this new file and add the following code

<?php

// $site/apps/frontend/lib/CompanyContextFilter.class.php
class CompanyContextFilter extends sfFilter{
	public function execute($filterChain){
		
		$uri = $this->context->getRequest()->getUri();
			
		$matches = array();
		preg_match('/http:\/\/([^\.]*)\.time-tracker\.ch/', $uri, $matches);
		$company = CompanyTable::getInstance()->findOneBySlug($matches[1]);

		if($company == null){
			 
			die(sprintf('{"result":%s}', ResponseCode::COMPANY_NOT_FOUND));

		}

		$this->context->set('Company', $company);
		
		$filterChain->execute($filterChain);

	}
}

This code is taken from the Time Tracker application's api "application", modified for readability. Here is a break down:

  • Line 7: Simply fetches the URL
  • Line 10: Extracts the company slug
  • Line 11: Searches the Company table for a company matching that slug
  • Line 19: If all is well, adds the Company object in to the context. 
  • Line 21: Calls the all important execute() method on the sfFilterChain to keep the flow going

The company object is now available for the main execution logic.

Register the filter with Symfony

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

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

rendering: ~
security:  ~

CompanyContextFilter:
    class: CompanyContextFilter

# other filters        

cache:     ~
execution: ~

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.

 

Read Back: Symfony: Execution Filters

Read on: Symfony: Execution Filters: Controlling output

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

No comments yet. Please leave a comment using the form below

Add a comment

*
*
Your email will not be published
*
*