Zend_Controller-ActionHelpers-Json.xml 4.5 KB

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