Zend_View-Controllers.xml 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.view.controllers">
  4. <title>Controller Scripts</title>
  5. <para>
  6. The controller is where you instantiate and configure <classname>Zend_View</classname>.
  7. You then assign variables to the view, and tell the view to
  8. render output using a particular script.
  9. </para>
  10. <sect2 id="zend.view.controllers.assign">
  11. <title>Assigning Variables</title>
  12. <para>
  13. Your controller script should assign necessary variables to the view
  14. before it hands over control to the view script. Normally, you
  15. can do assignments one at a time by assigning to property names
  16. of the view instance:
  17. </para>
  18. <programlisting language="php"><![CDATA[
  19. $view = new Zend_View();
  20. $view->a = "Hay";
  21. $view->b = "Bee";
  22. $view->c = "Sea";
  23. ]]></programlisting>
  24. <para>
  25. However, this can be tedious when you have already collected the
  26. values to be assigned into an array or object.
  27. </para>
  28. <para>
  29. The assign() method lets you assign from an array or object "in
  30. bulk". The following examples have the same effect as the above
  31. one-by-one property assignments.
  32. </para>
  33. <programlisting language="php"><![CDATA[
  34. $view = new Zend_View();
  35. // assign an array of key-value pairs, where the
  36. // key is the variable name, and the value is
  37. // the assigned value.
  38. $array = array(
  39. 'a' => "Hay",
  40. 'b' => "Bee",
  41. 'c' => "Sea",
  42. );
  43. $view->assign($array);
  44. // do the same with an object's public properties;
  45. // note how we cast it to an array when assigning.
  46. $obj = new StdClass;
  47. $obj->a = "Hay";
  48. $obj->b = "Bee";
  49. $obj->c = "Sea";
  50. $view->assign((array) $obj);
  51. ]]></programlisting>
  52. <para>
  53. Alternatively, you can use the assign method to assign
  54. one-by-one by passing a string variable name, and then the
  55. variable value.
  56. </para>
  57. <programlisting language="php"><![CDATA[
  58. $view = new Zend_View();
  59. $view->assign('a', "Hay");
  60. $view->assign('b', "Bee");
  61. $view->assign('c', "Sea");
  62. ]]></programlisting>
  63. </sect2>
  64. <sect2 id="zend.view.controllers.render">
  65. <title>Rendering a View Script</title>
  66. <para>
  67. Once you have assigned all needed variables, the controller
  68. should tell <classname>Zend_View</classname> to render a particular view script.
  69. Do so by calling the render() method. Note that the method will
  70. return the rendered view, not print it, so you need to print or
  71. echo it yourself at the appropriate time.
  72. </para>
  73. <programlisting language="php"><![CDATA[
  74. $view = new Zend_View();
  75. $view->a = "Hay";
  76. $view->b = "Bee";
  77. $view->c = "Sea";
  78. echo $view->render('someView.php');
  79. ]]></programlisting>
  80. </sect2>
  81. <sect2 id="zend.view.controllers.script-paths">
  82. <title>View Script Paths</title>
  83. <para>
  84. By default, <classname>Zend_View</classname> expects your view scripts to be relative to
  85. your calling script. For example, if your controller script is at
  86. "/path/to/app/controllers" and it calls
  87. $view->render('someView.php'), <classname>Zend_View</classname> will look for
  88. "/path/to/app/controllers/someView.php".
  89. </para>
  90. <para>
  91. Obviously, your view scripts are probably located elsewhere. To
  92. tell <classname>Zend_View</classname> where it should look for view scripts, use the
  93. setScriptPath() method.
  94. </para>
  95. <programlisting language="php"><![CDATA[
  96. $view = new Zend_View();
  97. $view->setScriptPath('/path/to/app/views');
  98. ]]></programlisting>
  99. <para>
  100. Now when you call $view->render('someView.php'), it will look
  101. for "/path/to/app/views/someView.php".
  102. </para>
  103. <para>
  104. In fact, you can "stack" paths using the addScriptPath()
  105. method. As you add paths to the stack, <classname>Zend_View</classname> will look
  106. at the most-recently-added path for the requested view
  107. script. This allows you override default views with custom
  108. views so that you may create custom "themes" or "skins" for
  109. some views, while leaving others alone.
  110. </para>
  111. <programlisting language="php"><![CDATA[
  112. $view = new Zend_View();
  113. $view->addScriptPath('/path/to/app/views');
  114. $view->addScriptPath('/path/to/custom/');
  115. // now when you call $view->render('booklist.php'), Zend_View will
  116. // look first for "/path/to/custom/booklist.php", then for
  117. // "/path/to/app/views/booklist.php", and finally in the current
  118. // directory for "booklist.php".
  119. ]]></programlisting>
  120. <note>
  121. <title>Never use user input to set script paths</title>
  122. <para>
  123. <classname>Zend_View</classname> uses script paths to lookup and render
  124. view scripts. As such, these directories should be known
  125. before-hand, and under your control. <emphasis>Never</emphasis>
  126. set view script paths based on user input, as you can
  127. potentially open yourself up to Local File Inclusion
  128. vulnerability if the specified path includes parent directory
  129. traversals. For example, the following input could trigger the
  130. issue:
  131. </para>
  132. <programlisting language="php"><![CDATA[
  133. // $_GET['foo'] == '../../../etc'
  134. $view->addScriptPath($_GET['foo']);
  135. $view->render('passwd');
  136. ]]></programlisting>
  137. <para>
  138. While this example is contrived, it does clearly show the
  139. potential issue. If you <emphasis>must</emphasis> rely on user
  140. input to set your script path, properly filter the input and
  141. check to ensure it exists under paths controlled by your
  142. application.
  143. </para>
  144. </note>
  145. </sect2>
  146. </sect1>
  147. <!--
  148. vim:se ts=4 sw=4 et:
  149. -->