Using Extjs 4 as a Rest Client to Zend

As the Zend framework uses MVC and Extjs 4 is heavily pushing their new MVC architecture the two working together should be a good fit... Unfortunately the default url formats are slightly different, but enough to break out of the box compatibility. I won't delve into the reasons, rather read this.

Zend:
Using the Zend_Rest_Route framework router enables an application to receive and handle REST actions (PUT, POST, DELETE, GET) in a standardised method and expects a Url in the format of (simple key):
http:/www.example.com/{module}/{controller}/{idkey}/{idvalue}

Extjs:
Is able to send the standard REST verbs, but by default the url is of the format:
http:/www.example.com/{module}/{controller}/{idvalue}
Thus we end up with the two frameworks being unable to communicate (using the standard REST protocol anyway).

The solution:

The simplest solution I found so far was to create a Zend specific version of the Extjs Rest proxy (Ext.data.proxy.Rest). The specific method that needs to be overridden is buildUrl, and while the extjs documentation suggests where to start it does not follow through with an example.

The following javascript class will create a Zend specific Extjs proxy. In this instance the primary key is hardcoded as "id", so this should either be customised for your implementation or the class modified with the primary key passed as a configuration parameter which would give the most flexibility for re-use. Complex keys would need additional work...
Usage:
To use the class, include it in your project and configure your store (or model) to use the "zest" proxy.

From there on, whenever a store sends a PUT/POST request to the server it should use the correct url as required by Zend.


As a final passing note...
One thing I found once I had implemented this change was that for some reason even though I was sending the PUT data correctly from the UI (as verified by firebug), Zend was picking up the data, using the correct module and controller, but the PUT data was not being correctly interpreted (I am using Json) into individual request parameters.
Further research led me to this article
Responding to Different Content Types in Restful Zf Apps,
and finally
"Content-Type" header of request in Php


1 comment: