Zend_Controller-Router-Route-Rest.xml 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect3 id="zend.controller.router.routes.rest">
  4. <title>Zend_Rest_Route</title>
  5. <para>
  6. The <classname>Zend_Rest</classname> component contains a RESTful route
  7. for <classname>Zend_Controller_Router_Rewrite</classname>. This route
  8. offers a standardized routing scheme that routes requests by translating
  9. the <acronym>HTTP</acronym> method and the <acronym>URI</acronym>
  10. to a module, controller, and action. The table below provides an overview
  11. of how request methods and <acronym>URI</acronym>'s are routed.
  12. </para>
  13. <table frame="all">
  14. <title>Zend_Rest_Route Behavior</title>
  15. <tgroup cols='3' align='left' colsep='1' rowsep='1'>
  16. <colspec colname='method' />
  17. <colspec colname='URI' />
  18. <colspec colname='route' />
  19. <thead>
  20. <row>
  21. <entry>Method</entry>
  22. <entry><acronym>URI</acronym></entry>
  23. <entry>Module_Controller::action</entry>
  24. </row>
  25. </thead>
  26. <tbody>
  27. <row>
  28. <entry><constant>GET</constant></entry>
  29. <entry><filename>/product/ratings/</filename></entry>
  30. <entry><methodname>Product_RatingsController::indexAction()</methodname></entry>
  31. </row>
  32. <row>
  33. <entry><constant>GET</constant></entry>
  34. <entry><filename>/product/ratings/:id</filename></entry>
  35. <entry><methodname>Product_RatingsController::getAction()</methodname></entry>
  36. </row>
  37. <row>
  38. <entry><constant>POST</constant></entry>
  39. <entry><filename>/product/ratings</filename></entry>
  40. <entry><methodname>Product_RatingsController::postAction()</methodname></entry>
  41. </row>
  42. <row>
  43. <entry><constant>PUT</constant></entry>
  44. <entry><filename>/product/ratings/:id</filename></entry>
  45. <entry><methodname>Product_RatingsController::putAction()</methodname></entry>
  46. </row>
  47. <row>
  48. <entry><constant>DELETE</constant></entry>
  49. <entry><filename>/product/ratings/:id</filename></entry>
  50. <entry>
  51. <methodname>Product_RatingsController::deleteAction()</methodname>
  52. </entry>
  53. </row>
  54. <row>
  55. <entry><constant>POST</constant></entry>
  56. <entry><filename>/product/ratings/:id _method="PUT"</filename></entry>
  57. <entry><methodname>Product_RatingsController::putAction()</methodname></entry>
  58. </row>
  59. <row>
  60. <entry><constant>POST</constant></entry>
  61. <entry><filename>/product/ratings/:id _method="DELETE"</filename></entry>
  62. <entry>
  63. <methodname>Product_RatingsController::deleteAction()</methodname>
  64. </entry>
  65. </row>
  66. </tbody>
  67. </tgroup>
  68. </table>
  69. <sect4 id="zend.rest.route_usage">
  70. <title>Zend_Rest_Route Usage</title>
  71. <para>
  72. To enable <classname>Zend_Rest_Route</classname> for an entire
  73. application, construct it with no config params and add it as the
  74. default route on the front controller:
  75. </para>
  76. <programlisting language="php"><![CDATA[$front = Zend_Controller_Front::getInstance();
  77. $restRoute = new Zend_Rest_Route($front);
  78. $front->getRouter()->addRoute('default', $restRoute);
  79. ]]></programlisting>
  80. <note>
  81. <para>
  82. If <classname>Zend_Rest_Route</classname> cannot match a valid
  83. module, controller, or action, it will return <constant>FALSE</constant> and the router
  84. will attempt to match using the next route in the router.
  85. </para>
  86. </note>
  87. <para>
  88. To enable <classname>Zend_Rest_Route</classname> for specific modules,
  89. construct it with an array of module names as the 3rd constructor
  90. argument:
  91. </para>
  92. <programlisting language="php"><![CDATA[$front = Zend_Controller_Front::getInstance();
  93. $restRoute = new Zend_Rest_Route($front, array(), array('product'));
  94. $front->getRouter()->addRoute('rest', $restRoute);
  95. ]]></programlisting>
  96. <para>
  97. To enable <classname>Zend_Rest_Route</classname> for specific
  98. controllers, add an array of controller names as the value of each
  99. module array element.
  100. </para>
  101. <programlisting language="php"><![CDATA[$front = Zend_Controller_Front::getInstance();
  102. $restRoute = new Zend_Rest_Route($front, array(), array(
  103. 'product' => array('ratings')
  104. ));
  105. $front->getRouter()->addRoute('rest', $restRoute);
  106. ]]></programlisting>
  107. </sect4>
  108. <sect4 id="zend.rest.route_config">
  109. <title>Zend_Rest_Route with Zend_Config_Ini</title>
  110. <para>
  111. To use Zend_Rest_Route from an INI config file, use a route type
  112. parameter and set the config options:
  113. </para>
  114. <programlisting language="ini"><![CDATA[routes.rest.type = Zend_Rest_Route
  115. routes.rest.defaults.controller = object
  116. routes.rest.mod = project,user
  117. ]]></programlisting>
  118. <para>The 'type' option designates the RESTful routing config type.
  119. The 'defaults' option is used to specify custom default
  120. module, controller, and/or actions for the route. All other options
  121. in the config group are treated as RESTful module names, and their
  122. values are RESTful controller names. The example config defines
  123. Mod_ProjectController and Mod_UserController as RESTful controllers.</para>
  124. <para>Then use the addConfig() method of the Rewrite router object:</para>
  125. <programlisting language="php"><![CDATA[$config = new Zend_Config_Ini('path/to/routes.ini');
  126. $router = new Zend_Controller_Router_Rewrite();
  127. $router->addConfig($config, 'routes');
  128. ]]></programlisting>
  129. </sect4>
  130. <sect4 id="zend.rest.controller">
  131. <title>Zend_Rest_Controller</title>
  132. <para>
  133. To help or guide development of Controllers for use with
  134. <classname>Zend_Rest_Route</classname>, extend your Controllers from
  135. <classname>Zend_Rest_Controller</classname>.
  136. <classname>Zend_Rest_Controller</classname> defines the 5 most-commonly
  137. needed operations for RESTful resources in the form of abstract action
  138. methods.
  139. </para>
  140. <itemizedlist>
  141. <listitem>
  142. <para>
  143. <emphasis><methodname>indexAction()</methodname></emphasis> -
  144. Should retrieve an index of resources and assign it to view.
  145. </para>
  146. </listitem>
  147. <listitem>
  148. <para>
  149. <emphasis><methodname>getAction()</methodname></emphasis> -
  150. Should retrieve a single resource identified by <acronym>URI</acronym>
  151. and assign it to view.
  152. </para>
  153. </listitem>
  154. <listitem>
  155. <para>
  156. <emphasis><methodname>postAction()</methodname></emphasis> -
  157. Should accept a new single resource and persist its state.
  158. </para>
  159. </listitem>
  160. <listitem>
  161. <para>
  162. <emphasis><methodname>putAction()</methodname></emphasis> -
  163. Should accept a single resource idenitifed by <acronym>URI</acronym>
  164. and persist its state.
  165. </para>
  166. </listitem>
  167. <listitem>
  168. <para>
  169. <emphasis><methodname>deleteAction()</methodname></emphasis> -
  170. Should delete a single resource identified by <acronym>URI</acronym>.
  171. </para>
  172. </listitem>
  173. </itemizedlist>
  174. </sect4>
  175. </sect3>
  176. <!--
  177. vim:se ts=4 sw=4 et:
  178. -->