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.