Zend Framework action assistant Json usage instance analysis, zendjson
This example describes how to use Zend Framework action assistant Json. We will share this with you for your reference. The details are as follows:
Json is easy to use. The following describes how to use Json:
Json is used to decode and send JSON responses;
When processing an AJAX request that expects the data table to respond, the JSON response quickly becomes the selected response.
JSON can be parsed on the client immediately for fast execution.
The JSON action assistant completes the following tasks:
If the layout is enabled, disable it.
If the view parser is open, close it.
Set the 'content-type' response header to 'application/json '.
By default, you do not need to wait for the action to complete and return a response immediately.
The usage is very simple: Call it as a helper proxy method, or call one of the encodeJson () and sendJson () methods:
class FooController extends Zend_Controller_Action{ public function barAction() { // do some processing... // Send the JSON response: $this->_helper->json($data); // or... $this->_helper->json->sendJson($data); // or retrieve the json: $json = $this->_helper->json->encodeJson($data); }}
Note: Keeping Layouts)
If you have a separate layout for the JSON response-you may encapsulate JSON into some contexts-each method in the JSON assistant accepts the second optional parameter: enable or disable the layout flag, passing a Boolean true value will keep the layout open:
class FooController extends Zend_Controller_Action{ public function barAction() { // Retrieve the json, keeping layouts: $json = $this->_helper->json->encodeJson($data, true); }}