Home >Backend Development >PHP Tutorial >Zend Framework action assistant Json usage example analysis, zendjson_PHP tutorial
This article describes the Zend Framework action assistant Json usage example. Share it with everyone for your reference, the details are as follows:
The use of Json is relatively simple. The following is the usage given in the document:
Json is used to decode and send JSON responses;
When handling AJAX requests that expect a data table response, JSON responses quickly become the response of choice.
JSON can be parsed immediately on the client side for fast execution.
JSON action assistant completes the following tasks:
If the layout is enabled, disable it.
If the ViewRenderer is open, close it.
Set the 'Content-Type' response header to 'application/json'.
By default, there is no need to wait for the action to be executed and the response is returned immediately.
Usage is simple: either call it as a method of the helper proxy, 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 - perhaps encapsulating the JSON into some context - each method in the JSON helper accepts a second optional argument: a flag to turn the layout on or off, 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); } }
Readers who are interested in more zend-related content can check out the special topics of this site: "Zend FrameWork Framework Introductory Tutorial", "php Excellent Development Framework Summary", "Yii Framework Introduction and Summary of Common Techniques", "ThinkPHP Introductory Tutorial" , "php object-oriented programming introductory tutorial", "php mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone in PHP programming.