Front Controller Design Pattern

One way to more efficiently and securely manage a web application is to implement the Front Controller design pattern. This pattern calls for a single object to route web traffic: This means there must be an entry point (commonly called the bootstrap) where the application defers control to the Front Controller object.

Bootstrap

In modern programming parlance, a bootstrap is a (usually short) file that contains the essential elements to launch a program. For an application built with the Istarel Workshop Application Framework, the bootstrap is index.php (located at the root level of the application directory), which integrates a constants file and two key objects:

  • Application Constants: This file includes application database and filepath information.
  • ApplicationDelegate: In addition to acting as an autoloader for application classes, this object also handles requests that apply to the entire web application.
  • ApplicationRunner: This object (the actual "Front Controller") identifies the page request and acts on it, with a little help from the ApplicationDelegate.

Listing: /iw/index.php

require 'conf/ApplicationConstants.php';
require 'rsrc/ApplicationDelegate.php';

ini_set('session.cookie_path', '/');
date_default_timezone_set('America/New_York');

require FRAMEWORK_DIR . 'ApplicationRunner.php';

$app = new ApplicationRunner;
$app->go();

All Roads Lead to index.php

In order for our bootstrap to work, and for the Front Controller to get all the information it needs, I first have to ensure that all the traffic to the application is routed through a single place: in this case, index.php. That "forced routing" is handled by the mod_rewrite extension to Apache. The mechanism I have chosen to handle this is to create httpd.conf files at the root level of each application folder.

Listing: /iw/httpd.conf

<Directory /Users/markf/Sites/iw/>
    RewriteEngine On
    RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
</Directory>

The redirect rule is a fairly simple one, and it essentially tells Apache to route every request for a file (that does not end in .js, .ico, etc.) to index.php. In order for this to work, Apache has to know about this file, so I reference this configuration file at the very end of Apache's main configuration file.

End of Listing: /usr/local/apache2/conf/httpd.conf

## Appendix: Directory-specific Directives
Include /Users/markf/Sites/iw/httpd.conf

As always, Apache must be restarted for changes in its configuration file to take effect.

Application Runner (the Front Controller)

ApplicationRunner uses the information in the URL to identify the active application module, and then instantiates the workflow that will manage the module. That workflow can be one of many different types (including unique ones defined in the application itself, if desired) and it acts as a mediator between the module and its associated model object and view (using mode and controller objects to simplify the process).