Zend_Controller-ActionHelpers-Json.xml 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. JSON responses are rapidly becoming the response of choice when dealing
  7. with AJAX requests that expect dataset responses; JSON can be
  8. immediately parsed on the client-side, leading to quick execution.
  9. </para>
  10. <para>
  11. The JSON action helper does several things:
  12. </para>
  13. <itemizedlist>
  14. <listitem><para>
  15. Disables layouts if currently enabled.
  16. </para></listitem>
  17. <listitem>
  18. <para>
  19. Optionally, an array of options to pass as the second argument
  20. to <classname>Zend_Json::encode()</classname>. This array of options
  21. allows enabling layouts and encoding using
  22. <classname>Zend_Json_Expr</classname>.
  23. </para>
  24. <programlisting role="php"><![CDATA[
  25. $this->_helper->json($data, array('enableJsonExprFinder' => true));
  26. ]]></programlisting>
  27. </listitem>
  28. <listitem><para>
  29. Disables the ViewRenderer if currently enabled.
  30. </para></listitem>
  31. <listitem><para>
  32. Sets the 'Content-Type' response header to 'application/json'.
  33. </para></listitem>
  34. <listitem><para>
  35. By default, immediately returns the response, without waiting
  36. for the action to finish execution.
  37. </para></listitem>
  38. </itemizedlist>
  39. <para>
  40. Usage is simple: either call it as a method of the helper broker, or
  41. call one of the methods <code>encodeJson()</code> or
  42. <code>sendJson()</code>:
  43. </para>
  44. <programlisting role="php"><![CDATA[
  45. class FooController extends Zend_Controller_Action
  46. {
  47. public function barAction()
  48. {
  49. // do some processing...
  50. // Send the JSON response:
  51. $this->_helper->json($data);
  52. // or...
  53. $this->_helper->json->sendJson($data);
  54. // or retrieve the json:
  55. $json = $this->_helper->json->encodeJson($data);
  56. }
  57. }
  58. ]]></programlisting>
  59. <note>
  60. <title>Keeping Layouts</title>
  61. <para>
  62. If you have a separate layout for JSON responses -- perhaps to wrap
  63. the JSON response in some sort of context -- each method in the JSON
  64. helper accepts a second, optional argument: a flag to enable or
  65. disable layouts. Passing a boolean <code>true</code> value will keep
  66. layouts enabled:
  67. </para>
  68. <programlisting role="php"><![CDATA[
  69. $this->_helper->json($data, true);
  70. ]]></programlisting>
  71. <para>
  72. Optionally, you can pass an array as the second parameter. This
  73. array may contain a variety of options, including the
  74. <code>keepLayouts</code> option:
  75. </para>
  76. <programlisting role="php"><![CDATA[
  77. $this->_helper->json($data, array('keepLayouts' => true);
  78. ]]></programlisting>
  79. </note>
  80. <note>
  81. <title>Enabling encoding using Zend_Json_Expr</title>
  82. <para>
  83. <classname>Zend_Json::encode()</classname> allows the encoding of native JSON
  84. expressions using <code>Zend_Json_Expr</code> objects. This option
  85. is disabled by default. To enable this option, pass a boolean
  86. <code>true</code> value to the <code>enableJsonExprFinder</code>
  87. option:
  88. </para>
  89. <programlisting role="php"><![CDATA[
  90. $this->_helper->json($data, array('enableJsonExprFinder' => true);
  91. ]]></programlisting>
  92. <para>
  93. If you desire to do this, you <emphasis>must</emphasis> pass an
  94. array as the second argument. This also allows you to combine other
  95. options, such as the <code>keepLayouts</code> option. All such
  96. options are then passed to <classname>Zend_Json::encode()</classname>.
  97. </para>
  98. <programlisting role="php"><![CDATA[
  99. $this->_helper->json($data, array(
  100. 'enableJsonExprFinder' => true,
  101. 'keepLayouts' => true,
  102. ));
  103. ]]></programlisting>
  104. </note>
  105. </sect3>
  106. <!--
  107. vim:se ts=4 sw=4 et:
  108. -->