Monday, June 1, 2009

The Front Controller Pattern: Removing redundancy in web applications

Hello, everyone! I'm here posting from my new job. In my frist day I've been given a series of documents to study. Turns out that we'll be using a front-controller architecture.

In my previous job, I used an MVC approach to design our web applications. But I noticed that there was a lot of redundancy in the "main" scripts. Basically, I did the following:

  1. Define the used variables
  2. Access session data
  3. Call the appropriate method based on the current action
  4. Process the results


The only necessary part for each script is step 3. Call the appropriate method based on the current action. So what am I doing with a bunch of nearly identical scripts that have to be customized?

This is where the Front Controller pattern comes in.

The Front Controller Pattern

In a nutshell, a Front Controller is a single script that serves as a front-end for all your applications. All the other scripts are called through some parameter (usually defined in the URL as a query string variable, or some path to a "script").

The trick is that the front controller already handles authentication, session, etc. If you want to add, for example, a traffic control routine to limit the number of calls per second, you can do it in the front controller.

For more information about the Front Controller pattern, check out these links:

http://java.sun.com/blueprints/patterns/FrontController.html

http://w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5

http://www.ibm.com/developerworks/library/os-php-designptrns/

http://evolucionphp.blogspot.com/2007/09/front-controller-pattern.html (spanish)

No comments: