I have never used Zend before, and my most recent PHP experience was over three years ago, so I was hoping there would be good examples/tutorials. Fat chance. I found several other blogs that gave an account of their own personal trials (and in a couple of cases, successes), but nothing in the order of "official". To give credit, Chris Danielson's post was my starting point but left quite a bit out that I needed.
First, why if I specified "format=xml" in my URL did the view still get returned as JSON? This was unnerving to say the least. After doing some digging, I found the answer on the Zend Framework Manual. Specifically, the section titled "12.8.4.3.1. Default Contexts Available" mentions that you have to disable automatic JSON serialization. Minor detail? So finally my XML view start working when I ask them to...
Next, how the heck do I access the request body for a POST or PUT request? In Chris Danielson's blog, his example shows the following to get an incoming parameter:
$this->getRequest()->getParam($paramName)
This only works for the query string though! So again, I did some looking, and found this:
$this->getRequest()->getRawBody()
Makes sense...but again, would have been great to see an example of this somewhere. All of the blogs I looked at focused on GET requests...as if POST is never used. :-)
Once I had these issues ironed out, it was just a matter of implementation. But as I got further into development, I saw a pattern forming that seemed ideal for the Framework to handle. It occurred to me that - for each method handler - I was:
- Adding an action handler to context switch for XML and JSON
- Defining a common document structure for the response (view) to be returned, regardless of context.
- Wrapping the view in "resolvable" phtml documents that encode the document in the proper context.
Overall I would say that Zend Framework is a good choice if you're starting a new Web service project with PHP. It is under active development by numerous contributors and supports a large number of features out-of-the-box.
You can download the code here.
-aj
What is the copyright and license of your code ?
ReplyDeleteI saw your answers on StackOverflow, Do you have any suggestions without using Zend?
ReplyDeleteThanks
-Brad