Comb boilerplate
Comb is the name I’ve given to a specific method of structuring PHP web applications. I hesitate to refer to it as a framework though…it’s more like a technique. Still, I’ve settled on a small set of common files and Apache settings that form a sort of boilerplate that can drop into place then expand upon.
You can get all ~80 lines of Hello, World! glory here: tar.gz | zip
Or observe it in all its nekkidness below…
- vhost.conf
- prepend.php
- append.php
- index.php
- 404.php
- templates/layout.tpl
- templates/index.tpl
vhost.conf
The virtual host container typically starts out with this basic configuration (drop this code into httpd.conf):
1 2 3 4 5 6 7 8 9 10 11 12 13 | <VirtualHost *:80>
ServerName www.example.com
DocumentRoot /vhosts/example.com/www/
ErrorDocument 404 /404.php
<Directory ".">
AllowOverride None
Order allow,deny
Allow from all
php_value auto_prepend_file /vhosts/example.com/www/prepend.php
php_value auto_append_file /vhosts/example.com/www/append.php
</Directory>
</VirtualHost> |
prepend.php
This gets auto-included at the top of any PHP script that resides inside this virtual host
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php /** * initial values for output control */ $response = array( 'redirect' => null, 'view' => 'auto', 'layout' => 'layout.tpl', 'content' => null ); /** * Here is a good place to connect to the db, memcached, etc */ /** * start up the output buffer */ ob_start(); ob_implicit_flush(FALSE); ?> |
append.php
This is where the template rendering takes place. It cleans the buffer, decides if redirecting needs to take place, and does any other common clean up (closing database connections, logging, etc).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <?php /** * all templates reside in the /templates/ dir under the docroot */ chdir($_SERVER['DOCUMENT_ROOT'] . 'templates'); /** * either render some output, or redirect to the specified URL */ if (empty($response['redirect'])) { /** * render the view template, if it was specified */ if (!empty($response['view'])) { if ($response['view'] == 'auto') { require trim($_SERVER['PHP_SELF'], ' /') . '.tpl'; } else { require $response['view']; } } $response['content'] = ob_get_clean(); require $response['layout']; } else { header("Location: $response[redirect]"); } ?> |
index.php
Gotta have at least one script for prepend and append to get (pre|ap)pended to. Predictably…
1 2 3 4 | <?php $title = 'Hello, World!'; $response['view'] = 'index.tpl'; ?> |
404.php
I just echo the error straight from this script…it’ll still get rendered within the layout template so it looks consistent with the rest of the site. If it makes you unhappy, you have the freedom to separate the actual error into it’s own template, like templates/404.tpl.
1 2 3 4 | <?php echo "<h1>OMG 404</h1>Here's some clues..."; var_dump($_SERVER); ?> |
templates/layout.tpl
The common, site-wide layout.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title><?=$title?></title> </head> <body> <h1>Example Site</h1> <div id="content"> <?=$response['content']?> </div> </body> </html> |
templates/index.tpl
Presentation logic for the homepage!
1 2 | This is the homepage, whose title is
<b><?=$title?></b> |