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…)
Posted in Evolving Framework, PHP | 1 Comment »
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…)
Posted in Evolving Framework, PHP | No Comments »
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…)
Posted in Evolving Framework, PHP | No Comments »
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.
Posted in Evolving Framework, PHP | 2 Comments »