Camber - a minimalist LAMP framework

Friday, June 8th, 2007

Over the past few months I’ve been adding code to this previously unnamed LAMP framework, but I’ve honestly been reluctant to call it a framework. That’s what it is though, so instead of constantly referring to it as “my little minimalist pseudo-framework”, I decided to give the thing a name. I’ve been mulling it over for several months now, and finally settled on Camber as the name. I wanted to be sure that it was easy enough to remember, but also meaningful to the overall goal of the project. According to Google:

Camber: The curvature of a structural member for the purpose of offsetting the deflection when loads are applied.

Camber is not a complex idea, nor is it difficult to implement where it’s needed. It’s used in many everyday load-bearing applications; the wheels of a car, the shape of skis and snowboards, the bottom of a bridge, the shape of a wing. The performance of each of these is greatly enhanced - if not utterly dependent - on something as simple as the underlying negative curvature.

Sounds like a pretty good description of a web application framework, eh?

Unfortunately, I’ve been really slack about keeping this here blog up to date with the actual code I’ve been writing. I have a tendency to drone on and on about each concept of the system, so I think I need to take smaller bites. One post per day maybe, with no more than 2 or 3 snippets of code.

Evolving Framework, Step 4: Handle requests with a request handler

Sunday, April 29th, 2007

So, picking up from where I left off in Step 3, cleaning up the server-side, we’ve now got a simple HTML form that sends a clean request into the server, and a nice little Request class to make the incoming data easy to work with. To jog your memory, the form looks like:
(more…)

Evolving Framework, Step 3: Cleaning up the server-side

Tuesday, January 30th, 2007

Once I had my virtual host set up to route all requests into a single script, I then had to figure out what to do with each request. I knew that the essential request information was available in the $_SERVER autoglobal, but I wanted a cleaner way of dealing with the request. So I put together a very simple Request class, which I saved in the document root as Request.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php 
class Request 
{ 
    public $method; 
    public $url; 
    public $parameters = array();  
    public function __construct() 
    { 
        $this->method = $_SERVER['REQUEST_METHOD']; 
        $this->url = $_SERVER['SCRIPT_URI']; 
        $this->parameters = $_REQUEST; 
    } 
} 
?>

I then altered index.php to contain the following:

1
2
3
4
5
6
7
8
9
10
<?php
    include 'Request.php';
 
    $req = new Request();   
 
    echo "Method: {$req->method} <br />";
    echo "URL: {$req->url} <br />";
    echo "Parameters: <code>" . print_r($req->parameters, true) . "</code>";
 
?>

(more…)

Evolving Framework, Step 2: Make PHP understand the clean URL

Sunday, January 28th, 2007

So why am I bothering with “clean” URLs? For several reasons really. For one thing, they look nicer than un-clean URLs. I would much rather get rid of something like, this:

http://www.example.com/forumDisplay.php?fid=17

and instead use something like this:

http://www.example.com/forum/general

But aesthetics aside, why should I bother? My reason is inspired by Roy Fielding’s REST. I won’t try to get into the guts of it here, partly because there’s alot to it, and partly because my grasp of it is far from complete. Regardless, what I’ve taken from my brief introduction to it is a resolution to build applications from a resource-centric point of view, as opposed to a functional, process-centric point of view. Instead of building a collection of scripts that expect data to be passed in so that they can spit data out, I decided to try to build a system of resources that can be manipulated.
(more…)

Evolving Framework, Step 1: mod_rewrite + PHP = Clean URLs

Saturday, January 27th, 2007

Using clean URLs with Apache is fairly simple with mod_rewrite’s help. I’d be the first to admit that rewrite rules can be as ridiculously complicated as they are powerful, but plenty of people have already admitted this. Luckily, what I’m trying to accomplish is not very difficult, mod_rewrite-wise. Taking a forum as an example, you might see URLs like:

  • http://www.example.com/ - site’s main page
  • http://www.example.com/forum - list of forums
  • http://www.example.com/forum/general - list all threads in general forum
  • http://www.example.com/forum/general/some_arbitrary_topic - list all posts in some_arbitrary_topic thread

To handle URLs similar to this, I set up the virtual host container to simply rewrite every request through a single PHP script, which I’ll lovingly call index.php. To start off, it just looks like this:

1
2
3
<code>
<?php print_r($_SERVER); ?>
</code>

(more…)

FINE…I’ll start a blog to explain my code to you, Wess.

Thursday, January 25th, 2007

I’ve lost count of the number of times I’ve created a blog and started off posting stuff to it, only to get slack and let the thing fester for months and, inevitably, deleted it. This particular time I’ve got a decent reason that I think will help me keep the thing going. I’ve been working on a project - some might call it a framework - to make building web applications in PHP a little more tolerable. I’ve built half-assed frameworks before for the guts of various web sites, but I seem to have a bad habit of falling into a deep dark state of despair with my code. It’s like I work through the Gee, this is fun and new and interesting! phase and too quickly get to the Lord, this wasn’t such a good idea, when will this be over? phase…and I make that transition before the project gets any where near being complete.

So I decided to try starting from scratch. Here’s what I wanted to accomplish:

  • Very close proximity to HTTP. I hesitate say RESTful because that’s a pretty high benchmark, but that’s sort of the idea. 
  • Implicit support for clean URLs. Heck, make them required.
  • Built-in, pure PHP templating. I want to reap all the benefits of APC without having to compile my own made-up template language to PHP.
  • Barebones. My overall goal is just finding a decent way to organize all the code, so I’m ignoring things like I18N, PHP4 compatability, and support for shared hosting environments.

I’ve got some basic code working, but I’m not really pleased with all of it so it’s very much experimental.  I’ll try to relate my thought process in as logical a manner as I can muster over the next few days in an attempt to explain the thing.