Check out these API docs, they’ve got Resources implemented as directories. Each resource path contains two files – collection.php and item.php, which contain global methods with onGet() and onPost() handled in the collections file, and onGet() onPut() and onDelete() handled in the item file. The routing is Rails-style; “GET /people” to list, “POST /people” to create, “GET /people/1” to view an item, etc. The reference implementation shows raw SQL queries being serialized to JSON and rendered to the client.
The whole app, for a typical action, is about five lines of code, including db connection setup and JSON rendering, with precious little external configuration to depend on.
I always got a perverse joy in architecting my PHP applications so the filesystem provides the routing. Doing so pretty much stabs MVC in the heart, though, so I feel a lot more secure on Rails, with it’s models and glue. But something about the Project Zero architecture flips my bucket.
Is it that it would be easy for a new developer to ignore old code? – the resources are independent in implementation, they’re isolated in the filesystem, and they don’t require all those pesky MVC layers to do their job. I can see P.Zero applications being maintainable over the longhaul. If you need to change behaviour – rather than fix bugs – you can just add another resource that evidences the new behvaiour. Never worry about client code breakage again, just use versioned urls.
Oh but we’ll have to wait and see how it actually pans out. My Rails models are always being refactored to support new functionality. With Project Zero’s reference implementation you’ll rue the day you ever have to change database schemas. Of course, you could use Zero with a model layer, and abstract the queries from the listeners. Of course your code can get complex quickly in any framework. I’d be excited to see Ruby runtime that mirrors PHP’s filesystem approach. Lightweight and loosely-coupled, that’s how I like em.
It’s been a good year for REST so far. Rails is chugging along, and mod_atom is giving us another lightweight answer to REST. Anything that just lets me delegate it all to Apache is music to my ears. It would be super-sweet to stumble upon a RESTful XSFP store. Or a Rhino JSON Store, with REST on all the object nodes, and a client-side API to make it transparent. To dream…
Kudos to the P.Zero team for making the old new again. Lightweight PHP has a future, and it may be RESTful. REST has a future, and it’s everywhere.
2 comments on Project Zero gives me PHP envy.
very useful information, thanks alot
CouchDB (http://couchdb.org/CouchDB/CouchDBWeb.nsf/Home?OpenForm) is your dream come true. It is a RESTful JSON store which is “viewed” (fetched/transformed) using server-side Javascript. The Javascript is interpreted on SpiderMonkey. This may not be Rhino, but it is still a dream close true.