migration-06.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="migration.06">
  4. <title>Zend Framework 0.6</title>
  5. <para>
  6. When upgrading from a previous release to Zend Framework 0.6 or higher you
  7. should note the following migration notes.
  8. </para>
  9. <sect2 id="migration.06.zend.controller">
  10. <title>Zend_Controller</title>
  11. <para>
  12. The most basic usage of the <acronym>MVC</acronym> components has not changed; you can
  13. still do each of the following:
  14. </para>
  15. <programlisting language="php"><![CDATA[
  16. Zend_Controller_Front::run('/path/to/controllers');
  17. ]]></programlisting>
  18. <programlisting language="php"><![CDATA[
  19. /* -- create a router -- */
  20. $router = new Zend_Controller_RewriteRouter();
  21. $router->addRoute('user',
  22. 'user/:username',
  23. array('controller' => 'user', 'action' => 'info')
  24. );
  25. /* -- set it in a controller -- */
  26. $ctrl = Zend_Controller_Front::getInstance();
  27. $ctrl->setRouter($router);
  28. /* -- set controller directory and dispatch -- */
  29. $ctrl->setControllerDirectory('/path/to/controllers');
  30. $ctrl->dispatch();
  31. ]]></programlisting>
  32. <para>
  33. We encourage use of the Response object to aggregate content and
  34. headers. This will allow for more flexible output format switching
  35. (for instance, <acronym>JSON</acronym> or <acronym>XML</acronym> instead of
  36. <acronym>XHTML</acronym>) in your applications.
  37. By default, <methodname>dispatch()</methodname> will render the response, sending both
  38. headers and rendering any content. You may also have the front
  39. controller return the response using <methodname>returnResponse()</methodname>,
  40. and then render the response using your own logic. A future version
  41. of the front controller may enforce use of the response object via
  42. output buffering.
  43. </para>
  44. <para>
  45. There are many additional features that extend the existing <acronym>API</acronym>,
  46. and these are noted in the documentation.
  47. </para>
  48. <para>
  49. The main changes you will need to be aware of will be found when
  50. subclassing the various components. Key amongst these are:
  51. </para>
  52. <itemizedlist>
  53. <listitem>
  54. <para>
  55. <methodname>Zend_Controller_Front::dispatch()</methodname> by default
  56. traps exceptions in the response object, and does not render
  57. them, in order to prevent sensitive system information from
  58. being rendered. You can override this in several ways:
  59. </para>
  60. <itemizedlist>
  61. <listitem>
  62. <para>
  63. Set <methodname>throwExceptions()</methodname> in the front
  64. controller:
  65. </para>
  66. <programlisting language="php"><![CDATA[
  67. $front->throwExceptions(true);
  68. ]]></programlisting>
  69. </listitem>
  70. <listitem>
  71. <para>
  72. Set <methodname>renderExceptions()</methodname> in the response
  73. object:
  74. </para>
  75. <programlisting language="php"><![CDATA[
  76. $response->renderExceptions(true);
  77. $front->setResponse($response);
  78. $front->dispatch();
  79. // or:
  80. $front->returnResponse(true);
  81. $response = $front->dispatch();
  82. $response->renderExceptions(true);
  83. echo $response;
  84. ]]></programlisting>
  85. </listitem>
  86. </itemizedlist>
  87. </listitem>
  88. <listitem>
  89. <para>
  90. <methodname>Zend_Controller_Dispatcher_Interface::dispatch()</methodname>
  91. now accepts and returns a <link linkend="zend.controller.request">The
  92. Request Object</link> instead of a dispatcher token.
  93. </para>
  94. </listitem>
  95. <listitem>
  96. <para>
  97. <methodname>Zend_Controller_Router_Interface::route()</methodname>
  98. now accepts and returns a <link linkend="zend.controller.request">The
  99. Request Object</link> instead of a dispatcher token.
  100. </para>
  101. </listitem>
  102. <listitem>
  103. <para><classname>Zend_Controller_Action</classname> changes include:</para>
  104. <itemizedlist>
  105. <listitem>
  106. <para>
  107. The constructor now accepts exactly three arguments,
  108. <classname>Zend_Controller_Request_Abstract</classname>
  109. <varname>$request</varname>,
  110. <classname>Zend_Controller_Response_Abstract</classname>
  111. <varname>$response</varname>,
  112. and <type>Array</type> <varname>$params</varname> (optional).
  113. <methodname>Zend_Controller_Action::__construct()</methodname> uses
  114. these to set the request, response, and invokeArgs
  115. properties of the object, and if overriding the
  116. constructor, you should do so as well. Better yet, use
  117. the <methodname>init()</methodname> method to do any instance
  118. configuration, as this method is called as the final
  119. action of the constructor.
  120. </para>
  121. </listitem>
  122. <listitem>
  123. <para>
  124. <methodname>run()</methodname> is no longer defined as final, but is
  125. also no longer used by the front controller; its sole
  126. purpose is for using the class as a page controller. It
  127. now takes two optional arguments, a
  128. <classname>Zend_Controller_Request_Abstract</classname>
  129. <varname>$request</varname>
  130. and a <classname>Zend_Controller_Response_Abstract</classname>
  131. <varname>$response</varname>.
  132. </para>
  133. </listitem>
  134. <listitem>
  135. <para>
  136. <methodname>indexAction()</methodname> no longer needs to be
  137. defined, but is encouraged as the default action. This
  138. allows using the RewriteRouter and action controllers to
  139. specify different default action methods.
  140. </para>
  141. </listitem>
  142. <listitem>
  143. <para>
  144. <methodname>__call()</methodname> should be overridden to handle any
  145. undefined actions automatically.
  146. </para>
  147. </listitem>
  148. <listitem>
  149. <para>
  150. <methodname>_redirect()</methodname> now takes an optional second
  151. argument, the <acronym>HTTP</acronym> code to return with the redirect,
  152. and an optional third argument, <varname>$prependBase</varname>,
  153. that can indicate that the base <acronym>URL</acronym> registered with
  154. the request object should be prepended to the url specified.
  155. </para>
  156. </listitem>
  157. <listitem>
  158. <para>
  159. The <varname>$_action</varname> property is no longer set. This property
  160. was a <classname>Zend_Controller_Dispatcher_Token</classname>,
  161. which no longer exists in the current incarnation.
  162. The sole purpose of the token was to provide
  163. information about the requested controller, action,
  164. and <acronym>URL</acronym> parameters. This information is now
  165. available in the request object, and can be accessed
  166. as follows:
  167. </para>
  168. <programlisting language="php"><![CDATA[
  169. // Retrieve the requested controller name
  170. // Access used to be via: $this->_action->getControllerName().
  171. // The example below uses getRequest(), though you may also directly
  172. // access the $_request property; using getRequest() is recommended as
  173. // a parent class may override access to the request object.
  174. $controller = $this->getRequest()->getControllerName();
  175. // Retrieve the requested action name
  176. // Access used to be via: $this->_action->getActionName().
  177. $action = $this->getRequest()->getActionName();
  178. // Retrieve the request parameters
  179. // This hasn't changed; the _getParams() and _getParam() methods simply
  180. // proxy to the request object now.
  181. $params = $this->_getParams();
  182. // request 'foo' parameter, using 'default' as default value if not found
  183. $foo = $this->_getParam('foo', 'default');
  184. ]]></programlisting>
  185. </listitem>
  186. <listitem>
  187. <para>
  188. <methodname>noRouteAction()</methodname> has been removed. The
  189. appropriate way to handle non-existent action
  190. methods should you wish to route them to a default
  191. action is using <methodname>__call()</methodname>:
  192. </para>
  193. <programlisting language="php"><![CDATA[
  194. public function __call($method, $args)
  195. {
  196. // If an unmatched 'Action' method was requested, pass on to the
  197. // default action method:
  198. if ('Action' == substr($method, -6)) {
  199. return $this->defaultAction();
  200. }
  201. throw new Zend_Controller_Exception('Invalid method called');
  202. }
  203. ]]></programlisting>
  204. </listitem>
  205. </itemizedlist>
  206. </listitem>
  207. <listitem>
  208. <para>
  209. <methodname>Zend_Controller_RewriteRouter::setRewriteBase()</methodname> has
  210. been removed. Use <methodname>Zend_Controller_Front::setBaseUrl()</methodname>
  211. instead (or <methodname>Zend_Controller_Request_Http::setBaseUrl()</methodname>,
  212. if using that request class).
  213. </para>
  214. </listitem>
  215. <listitem>
  216. <para>
  217. <classname>Zend_Controller_Plugin_Interface</classname> was replaced
  218. by <classname>Zend_Controller_Plugin_Abstract</classname>. All methods now
  219. accept and return a <link linkend="zend.controller.request">The Request
  220. Object</link> instead of a dispatcher token.
  221. </para>
  222. </listitem>
  223. </itemizedlist>
  224. </sect2>
  225. </sect1>
  226. <!--
  227. vim:se ts=4 sw=4 et:
  228. -->