web.py style request dispatcher, in PHP

I try to write web.py style request dispatcher in PHP and see if I can make a use of it. So, what drives me to do this ? To quote Aaron in his post Rewriting Reddit regarding web.py:-

So how should things work? The first principle is that code should be clear and simple. If you want to output some text, you call web.output. If you want to get form input, you call web.input. There’s nothing particularly hard to remember.

The second principle is that web.py should fit your code, not the other way around. Every function in web.py is completely independent, you can use whichever ones you want. You can put your files wherever you like, and web.py will happily follow along. If you want a piece of code to be run as a web app, you call web.run, you don’t put your code in the magical place so that web.py can run you.

The third principle is that web.py should, by default, do the right thing by the Web. This means distinguishing between GET and POST properly. It means simple, canonical URLs which synonyms redirect to. It means readable HTML with the proper HTTP headers.

Before you take a look at the code, please note that:-

  • It use regex - some people would consider that as slow and ugly. /me not sure.
  • It require mod_rewrite for the clean url.
  • It’s an experimental. I don’t even use it anywhere in my apps.
  • It sucks - batteries not included ! You can’t even run the example. I wrote it on top of php framework that I derived from my previous company so you would see some require to unincluded file, ‘magic’ constants and methods coming from nowhere.

If you wish, you can take a look at http://www.k4ml.com/wiki/project/web.inc.php. For now, this is not on top of my prority. I would need to write or find something like form.py if I want to switch back to PHP. Lately, I have a feeling that phphtmllib’s Form APIs are sucks.

Update

Look’s like someone has built web.py alike framework in PHP. Look at tonic - http://tonic.sourceforge.net/