Ask the PHP gods and they shall provide...

A wealth of great information much appreciated. I look forward to the blog entries.

E

On Sep 7, 2009, at 11:11 AM, Matthew Weier O'Phinney wrote:

-- DorkFest <[email protected]> wrote
(on Monday, 07 September 2009, 07:28 AM -0700):
Is there any documentation on this component?

Can this controller be used to build an XML/JSON based API service? If so, is there a built in method for retrieving an XML request fragment to the controller (ie: not key/val pairs)? I think this is normally access with
something like: file_get_contents() from stdin. Wondering if ZF has a
built-in way.

You can pull in the raw request body from the request object:

   $body = $this->getRequest()->getRawBody();

With JSON data, it's trivial then to get your data:

   $data = Zend_Json::decode($body);

With XML, simply pass it to SimpleXML:

   $data = new SimpleXMLElement($body);

What I've done is to create an action helper that checks the "Accept"
request header ($request->getHeader('Accept')), and then sets the
"format" request parameter accordingly:

// Accept headers often are of format: "application/json; charset=utf-8"
   $accept = $request->getHeader('Accept');
   $accept = explode(';', $accept);
   $accept = trim(array_shift($accept));

   switch ($accept) {
       case "application/json":
           $request->setParam('format', 'json');
           break;
       case "application/xml":
           $request->setParam('format', 'xml');
           break;
   }

Put the above code in the preDispatch() method of your action helper,
and then register that helper with the helper broker in your bootstrap
to ensure it's run for each controller requested.

Then, in your controller, you can initialize the ContextSwitch helper:

   $context = $this->_helper->contextSwitch;
$context->setAutoJsonSerialization(false); // do this so you can have
                                              // JSON-specific views
   $context->addActionContext('index', 'xml')
           ->addActionContext('index', 'json')
           // ...

   $context->init();

If the context (xml or json, in the example above) is found (which will be based on the "format" set previously), then it will attempt to render
a different view script: index.xml.phtml or index.json.phtml,
respectively. This allows you to keep your controller lean, and have
view-specific logic for each format. (For instance, I like to set my
response headers, such as Content-Type, Content-Range, etc., within my
views.)

I'm surprised there are very few posts and almost no documentation. Does
anyone have info on this component?

Well, the documentation is primarily centered around the basic usage of the component, which is for creating RESTful resources in the MVC via a combination of routing and specific actions in your controller. How you
handle different content types is something else entirely. I'll be
blogging on some of this information in the coming months, and also
covering it during my "Architecting Ajax Applications in Zend Framework"
tutorial at ZendCon.

--
Matthew Weier O'Phinney
Project Lead            | [email protected]
Zend Framework          | http://framework.zend.com/

Reply via email to