njh.me - content negotiation in PHP

Hello,

Apologies for a bit a shameless self-promotion but I think it might be of
interest to this group.

I was recently looking at my WebID:
http://www.aelius.com/njh#me

And realised that I could have this instead and save a few bytes ;-)
http://njh.me/

So I bought njh.me and I am now using it as the identifier for me, while
keeping my homepage at http://www.aelius.com/njh/



Doing content negotiation properly is still badly supported by most
languages and environments. I wanted to use a lightweight MVC framework and
chose Slim. Slim has had content negotiation on its backlog for quite a long
time but I decided that if I wanted to get it working any time soon, I was
going to have to implement it myself.

My fork is here:
https://github.com/njh/Slim/tree/negotiation

And there is a pull request to bring it into master here:
https://github.com/codeguy/Slim/pull/376


The negotiation API is very simple and allows you to do this:
$format = $app->respondTo('html', 'rdf', 'ttl', 'json');

The list of suffixes is the file formats that are supported by the
application. They are ordered by the server's preference of format. The
client can either use an Accept header to select the desired format or force
a specific format by appending a suffix to the URL, for example:

http://njh.me/foaf.rdf

If you make a request with no Accept header and no suffix, then the first
format in the list will be returned (HTML). The client can use q values to
rank its preferred format.


The Slim application for njh.me is only 32 lines long and it hopefully very
easy to read:
https://github.com/njh/njh.me/blob/master/public/index.php

It depends upon Composer, Slim and EasyRdf. There are some PHPUnit tests to
ensure the it behaves correctly.

Web browsers requesting http://njh.me/ will be redirected, using a 303 to my
homepage. A Semweb clients setting a Accept: application/rdf+xml will be
sent a 303 redirect to http://njh.me/foaf.rdf, which in turn will return
RDF/XML. 


Hopefully this is a pattern that people can re-use and apply to more complex
examples. 


nick.


http://www.bbc.co.uk/
This e-mail (and any attachments) is confidential and may contain personal views which are not the views of the BBC unless specifically stated.
If you have received it in error, please delete it from your system.
Do not use, copy or disclose the information in any way nor act in reliance on it and notify the sender immediately.
Please note that the BBC monitors e-mails sent or received.
Further communication will signify your consent to this.
					

Received on Sunday, 9 September 2012 10:46:42 UTC