Tracking the tools that decentralize the media. tools process ideas resources eventsav

unmediated

 

April 06, 2005

When you program web applications in a RESTful style, you tend to use idioms from the HTTP specification. For example you're likely to return a broader variety off HTTP status codes, each one of which you'll have to grab from RFC 2616. Because this is repetitive it lends itself to being automated. I automated it for PHP applications by compiling a template application which contains all my standard idioms and constants. When I'm creating a new script I copy the template file over, delete the bits I don't need, and change application-specific constants like URIs. This saves a lot of time and prevents bugs. My template file has now been used for 10-15 different scripts, so I decided that it is grown-up enough to be useful to other people and took a few minutes to package the source for public release. As well as saving typing, it may also be handy as an illustration of RESTful techniques. Because code always needs a clear purpose, part of the packaging was to invent one. I gave it the purpose of returning the sum of its arguments. If you call the script with the URI /sum/1/1, it will return the value '2'. You can see it in action at http://gonze.com/sum or grab the source at http://gonze.com/resty/resty.php.