Zend_Controller-ActionHelpers-Json.xml 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect3 id="zend.controller.actionhelpers.json">
  4. <title>JSON</title>
  5. <para>
  6. <acronym>JSON</acronym> responses are rapidly becoming the response of choice when dealing
  7. with <acronym>AJAX</acronym> requests that expect dataset responses;
  8. <acronym>JSON</acronym> can be immediately parsed on the client-side, leading to quick
  9. execution.
  10. </para>
  11. <sect4 id="zend.controller.actionhelpers.json.usage">
  12. <title>Usage</title>
  13. <para>
  14. Usage is simple: either call it as a method of the helper broker, or
  15. call one of the methods <methodname>encodeJson()</methodname> or
  16. <methodname>sendJson()</methodname>:
  17. </para>
  18. <para>
  19. direct($data, $sendNow = true, $keepLayouts = false, $encodeData = true)
  20. </para>
  21. <para>
  22. sendJson($data, $keepLayouts = false, $encodeData = true)
  23. </para>
  24. <para>
  25. encodeJson($data, $keepLayouts = false, $encodeData = true)
  26. </para>
  27. <itemizedlist>
  28. <listitem>
  29. <para>
  30. <emphasis>$data</emphasis>: data to encode as JSON
  31. </para>
  32. </listitem>
  33. <listitem>
  34. <para>
  35. <emphasis>$sendNow</emphasis>: flag to define whether
  36. to send the JSON data immediately. When true, the helper
  37. will immediately set the respose body and exit.
  38. </para>
  39. </listitem>
  40. <listitem>
  41. <para>
  42. <emphasis>$keepLayouts</emphasis>: flag to define whether
  43. to enable or disable layours. When false, all layouts
  44. are disabled. Optionally, this can be an array of options
  45. to pass as the second argument to <methodname>Zend_Json::encode()</methodname>.
  46. This array of options allows enabling layouts and encoding using
  47. <classname>Zend_Json_Expr</classname>.
  48. </para>
  49. </listitem>
  50. <listitem>
  51. <para>
  52. <emphasis>$encodeData</emphasis>: flag to define whether
  53. <emphasis>$data</emphasis> is already JSON-encoded. When
  54. true, this helper will not encode <emphasis>$data</emphasis>
  55. to JSON before sending.
  56. </para>
  57. </listitem>
  58. </itemizedlist>
  59. <note>
  60. <title>Keeping Layouts</title>
  61. <para>
  62. If you have a separate layout for <acronym>JSON</acronym> responses -- perhaps to wrap
  63. the <acronym>JSON</acronym> response in some sort of context -- each method in the
  64. <acronym>JSON</acronym> helper accepts an optional argument <emphasis>$keepLayouts</emphasis>: a flag to enable or
  65. disable layouts. Passing a boolean <constant>TRUE</constant> value will keep
  66. layouts enabled:
  67. </para>
  68. <programlisting language="php"><![CDATA[
  69. $this->_helper->json($data, true);
  70. ]]></programlisting>
  71. <para>
  72. Optionally, you can pass an array as the third parameter. This
  73. array may contain a variety of options, including the
  74. <emphasis>keepLayouts</emphasis> option:
  75. </para>
  76. <programlisting language="php"><![CDATA[
  77. // Direct helper call
  78. $this->_helper->json($data, true, array('keepLayouts' => true);
  79. // ...or, call a method of the helper
  80. $this->_helper->sendJson($data, array('keepLayouts' => true));
  81. ]]></programlisting>
  82. </note>
  83. <note>
  84. <title>Enabling encoding using Zend_Json_Expr</title>
  85. <para>
  86. <methodname>Zend_Json::encode()</methodname> allows the encoding of native
  87. <acronym>JSON</acronym> expressions using <classname>Zend_Json_Expr</classname>
  88. objects. This option is disabled by default. To enable this option, pass a boolean
  89. <constant>TRUE</constant> value to the <emphasis>enableJsonExprFinder</emphasis>
  90. option:
  91. </para>
  92. <programlisting language="php"><![CDATA[
  93. $this->_helper->json($data, true, array('enableJsonExprFinder' => true);
  94. ]]></programlisting>
  95. <para>
  96. If you desire to do this, you <emphasis>must</emphasis> pass an
  97. array as the third argument. This also allows you to combine other
  98. options, such as the <emphasis>keepLayouts</emphasis> option. All such
  99. options are then passed to <methodname>Zend_Json::encode()</methodname>.
  100. </para>
  101. <programlisting language="php"><![CDATA[
  102. $this->_helper->json($data, true, array(
  103. 'enableJsonExprFinder' => true,
  104. 'keepLayouts' => true,
  105. ));
  106. ]]></programlisting>
  107. </note>
  108. </sect4>
  109. <sect4 id="zend.controller.actionhelpers.json.example">
  110. <title>Example</title>
  111. <programlisting language="php"><![CDATA[
  112. class FooController extends Zend_Controller_Action
  113. {
  114. public function barAction()
  115. {
  116. // do some processing...
  117. // Send the JSON response:
  118. $this->_helper->json($data);
  119. // or...
  120. $this->_helper->json->sendJson($data);
  121. // or retrieve the json:
  122. $json = $this->_helper->json->encodeJson($data);
  123. }
  124. }
  125. ]]></programlisting>
  126. </sect4>
  127. </sect3>
  128. <!--
  129. vim:se ts=4 sw=4 et:
  130. -->