Zend_Controller-ActionHelpers-Json.xml 4.4 KB

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