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: httpd.conf
<Directory /Users/markf/Sites/dblog/>
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.
One way to more efficiently and securely manage a web application is to take advantage of the Front Controller design pattern. This pattern calls for a single object to route web traffic: for the developerBlog (and other applications I build with the IWFrameworks), this means there must be an entry point for the application (often termed the bootstrap) and the Front Controller object to which it defers control.
End of Listing: /usr/local/apache2/conf/httpd.conf
## Appendix: Directory-specific Directives Include /Users/markf/Sites/dblog/httpd.conf
Bootstrapping
The bootstrap for an IWFrameworks-based application consists of a few include files and instantiation of an ApplicationRunner object, which acts as the front controller.
Bootstrap Components:
- 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 identifies the page request and acts on it, with a little help from the ApplicationDelegate.
Listing: /index.php
<?php
require 'rsrc/conf/ApplicationConstants.php';
require 'rsrc/ApplicationDelegate.php';
ini_set('session.cookie_path', APPL_ROOT_DIR);
require FRAMEWORK_DIR . 'ApplicationRunner.php';
$app = new ApplicationRunner;
$app->go();
?>
Front Controller
ApplicationRunner uses the page information to deduce the module that should be displayed, 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 view and its associated model object.
