Web Patterns: The Filter

Name

Filter

Context

Used for conditional code execution when want to respond conditionally to circumstances.

Problem

How to provide a mechanism for conditionally executing code at the start or end of each page request.

 

Forces

Motivation

Having the ability to conditionally run functions as part of a web page is a very powerful tool for web programmers.

Goals+Constraints

  • Configuring conditional code execution is easy.
  • Adding a new logging function or authentication filter is just a filter registration function call away.
  • Be careful of filter side effects. Filters can stop the rest of a page from completing execution or stop other filters from running. You need to pay special attention to the order that filters are registered and their likely return values.

Solution

Structure

  • Client requests page
  • Register filters
  • Check conditions on all post_auth filters and run if met
  • Run page script
  • Check conditions on all trace filters and run if met

Participants

None

Collaborations

The developer may register and run a number of post_auth and / or trace filters. The return values of these filter functions influence the continuing execution of the filters.

Resulting Context

Having the ability to conditionally run functions as part of a web page is a very powerful tool for web programmers. It gives us the flexibility to add and configure logging functions, authentication checks and so on. Filters control the registration and execution of these functions.