Zend_View-Helpers-Json.xml 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 24249 -->
  3. <!-- Reviewed: no -->
  4. <sect3 id="zend.view.helpers.initial.json">
  5. <title>L'aide de vue JSON</title>
  6. <para>
  7. Quand vous créez des vues qui retournent du <acronym>JSON</acronym>, il est important de paramétrer aussi
  8. les en-têtes de réponse appropriés. L'aide vue <acronym>JSON</acronym> réalise exactement cela. De plus, par
  9. défaut, elle désactive l'éventuel layout (s'il est activé), puisque les layouts sont
  10. rarement utilisés dans les réponses <acronym>JSON</acronym>.
  11. </para>
  12. <para>L'aide de vue <acronym>JSON</acronym> ajoute l'en-tête suivant :</para>
  13. <programlisting language="text"><![CDATA[
  14. Content-Type: application/json
  15. ]]></programlisting>
  16. <para>
  17. Beaucoup de librairies <acronym>AJAX</acronym> recherche cet en-tête quand elles analysent les réponses
  18. pour déterminer comment le contenu doit être géré.
  19. </para>
  20. <para>L'utilisation de l'aide de vue <acronym>JSON</acronym> est très simple :</para>
  21. <programlisting language="php"><![CDATA[
  22. <?php echo $this->json($this->data) ?>
  23. ]]></programlisting>
  24. <note>
  25. <title>Keeping layouts and enabling encoding using Zend_Json_Expr</title>
  26. <para>
  27. Each method in the <acronym>JSON</acronym> helper accepts a second, optional argument.
  28. This second argument can be a boolean flag to enable or disable
  29. layouts, or an array of options that will be passed to
  30. <methodname>Zend_Json::encode()</methodname> and used internally to encode data.
  31. </para>
  32. <para>
  33. To keep layouts, the second parameter needs to be boolean
  34. <constant>TRUE</constant>. When the second parameter is an array, keeping
  35. layouts can be achieved by including a <code>keepLayouts</code> key
  36. with a value of a boolean <constant>TRUE</constant>.
  37. </para>
  38. <programlisting language="php"><![CDATA[
  39. // Boolean true as second argument enables layouts:
  40. echo $this->json($this->data, true);
  41. // Or boolean true as "keepLayouts" key:
  42. echo $this->json($this->data, array('keepLayouts' => true));
  43. ]]></programlisting>
  44. <para>
  45. <classname>Zend_Json::encode</classname> allows the encoding of native <acronym>JSON</acronym>
  46. expressions using <classname>Zend_Json_Expr</classname> objects. This option
  47. is disabled by default. To enable this option, pass a boolean
  48. <constant>TRUE</constant> to the <code>enableJsonExprFinder</code> key of
  49. the options array:
  50. </para>
  51. <programlisting language="php"><![CDATA[
  52. <?php echo $this->json($this->data, array(
  53. 'enableJsonExprFinder' => true,
  54. 'keepLayouts' => true,
  55. )) ?>
  56. ]]></programlisting>
  57. </note>
  58. </sect3>