Thursday, January 29, 2009

ROA and PHP

According to the PHP Manual HTTP PUT is implemented as a sort of file upload emulation as though that was useful.

The whole subject of HTTP PUT was very murky until I read the RESTful Architecture O'Reilly book. What you need to know is that in a ROA (a Resource Orientated Architecture) uses HTTP PUT to create a resource.

Think social networking paths to individual resources requires a different function. In php it is analogous to:

HTTP PUT thing = make a file thing

You never want to make a file on a website. If you want a user to supply a resource this should be in a sequestered way, not in the actual pathways of the website.

With PHP there is a far more powerful way to implment HTTP PUT. First test that your brower is sending PUT and PHP is getting it.

Next, implement a resource appliction architecture that creates resources, not files. What is a resource? It is a set of objects you want to have exist on the server.

For example

GET /customer

returns a resource, in this case a list of customers

PUT /customer/1441

creates a resource, in this case customer 1411

PUT /customer/1411/name/Johnson

creates a customer whos name property is set to Johnson

Of course how a ROA is implemented in PHP is slightly different from the PHP manaul.

As a ROA maps against existing directories, you either will have to implement an extensive directory tree with index.phps at each level (preferably using a template like PHPTAL) for the views. But this is only good for STATIC objects. In other words for the "fixed" aspect that shape the system.

For example these resources can be fixed:

/customer
/supplier
/organization

But these resources must be dynamic:
/customer/1001
/supplier/new
/organization/staff/accounting/

Why? Because supporting or instantiating a directory structure for each entity is ridiculous. It is often method used for primitive resources: e.g. images contained in a resource mapped (i.e. customer_id in path) resource path. Often reflected by placing these images in files on disk.

Images will probably always just act like one dimensional arrays of bytes, accessed by a path name. The object handling is implicitly tied to system calls.

But if I want to dynamically create an object that has new functions being added to it - then its behaviour is not merely dump a file in a predictable way. The HTTP PUT carries a different set of semantics along with it, and this is handled in code.

So how do you access a resource on

/organizatation/staff/accounting/101

without it being a directory structure on the disk?

htaccess

URL Rewriting is an art that allows you to translate a dynamic predictable path like
/organization/staff/accounting/101

to

/organization/staff/accounting/customer.php?id=101

or

/orgaanization/staff.php?dept=accounting&customer_id=101

htaccess URL rewriting allows you to do this.

The rest of the ROA implementation using PUT to create a resource, GET to view it, POST to update it, DELETE to remove it - can be accomplished fairly easily.

No comments: