1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect3 id="zend.controller.actionhelpers.json">
7 <acronym>JSON</acronym> responses are rapidly becoming the response of choice when dealing
8 with <acronym>AJAX</acronym> requests that expect dataset responses;
9 <acronym>JSON</acronym> can be immediately parsed on the client-side, leading to quick
14 The <acronym>JSON</acronym> action helper does several things:
20 Disables layouts if currently enabled.
26 Optionally, an array of options to pass as the second argument
27 to <methodname>Zend_Json::encode()</methodname>. This array of options
28 allows enabling layouts and encoding using
29 <classname>Zend_Json_Expr</classname>.
32 <programlisting language="php"><![CDATA[
33 $this->_helper->json($data, array('enableJsonExprFinder' => true));
39 Disables the ViewRenderer if currently enabled.
45 Sets the 'Content-Type' response header to '<filename>application/json</filename>'.
51 By default, immediately returns the response, without waiting
52 for the action to finish execution.
58 Usage is simple: either call it as a method of the helper broker, or
59 call one of the methods <methodname>encodeJson()</methodname> or
60 <methodname>sendJson()</methodname>:
63 <programlisting language="php"><![CDATA[
64 class FooController extends Zend_Controller_Action
66 public function barAction()
68 // do some processing...
69 // Send the JSON response:
70 $this->_helper->json($data);
73 $this->_helper->json->sendJson($data);
75 // or retrieve the json:
76 $json = $this->_helper->json->encodeJson($data);
82 <title>Keeping Layouts</title>
85 If you have a separate layout for <acronym>JSON</acronym> responses -- perhaps to wrap
86 the <acronym>JSON</acronym> response in some sort of context -- each method in the
87 <acronym>JSON</acronym> helper accepts a second, optional argument: a flag to enable or
88 disable layouts. Passing a boolean <constant>TRUE</constant> value will keep
92 <programlisting language="php"><![CDATA[
93 $this->_helper->json($data, true);
97 Optionally, you can pass an array as the second parameter. This
98 array may contain a variety of options, including the
99 <emphasis>keepLayouts</emphasis> option:
102 <programlisting language="php"><![CDATA[
103 $this->_helper->json($data, array('keepLayouts' => true);
108 <title>Enabling encoding using Zend_Json_Expr</title>
111 <methodname>Zend_Json::encode()</methodname> allows the encoding of native
112 <acronym>JSON</acronym> expressions using <classname>Zend_Json_Expr</classname>
113 objects. This option is disabled by default. To enable this option, pass a boolean
114 <constant>TRUE</constant> value to the <emphasis>enableJsonExprFinder</emphasis>
118 <programlisting language="php"><![CDATA[
119 $this->_helper->json($data, array('enableJsonExprFinder' => true);
123 If you desire to do this, you <emphasis>must</emphasis> pass an
124 array as the second argument. This also allows you to combine other
125 options, such as the <emphasis>keepLayouts</emphasis> option. All such
126 options are then passed to <methodname>Zend_Json::encode()</methodname>.
129 <programlisting language="php"><![CDATA[
130 $this->_helper->json($data, array(
131 'enableJsonExprFinder' => true,
132 'keepLayouts' => true,