Zend_Controller-Router-Route-Rest.xml 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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[
  77. $front = Zend_Controller_Front::getInstance();
  78. $restRoute = new Zend_Rest_Route($front);
  79. $front->getRouter()->addRoute('default', $restRoute);
  80. ]]></programlisting>
  81. <note>
  82. <para>
  83. If <classname>Zend_Rest_Route</classname> cannot match a valid
  84. module, controller, or action, it will return <constant>FALSE</constant> and the router
  85. will attempt to match using the next route in the router.
  86. </para>
  87. </note>
  88. <para>
  89. To enable <classname>Zend_Rest_Route</classname> for specific modules,
  90. construct it with an array of module names as the 3rd constructor
  91. argument:
  92. </para>
  93. <programlisting language="php"><![CDATA[
  94. $front = Zend_Controller_Front::getInstance();
  95. $restRoute = new Zend_Rest_Route($front, array(), array('product'));
  96. $front->getRouter()->addRoute('rest', $restRoute);
  97. ]]></programlisting>
  98. <para>
  99. To enable <classname>Zend_Rest_Route</classname> for specific
  100. controllers, add an array of controller names as the value of each
  101. module array element.
  102. </para>
  103. <programlisting language="php"><![CDATA[
  104. $front = Zend_Controller_Front::getInstance();
  105. $restRoute = new Zend_Rest_Route($front, array(), array(
  106. 'product' => array('ratings')
  107. ));
  108. $front->getRouter()->addRoute('rest', $restRoute);
  109. ]]></programlisting>
  110. </sect4>
  111. <sect4 id="zend.rest.route_config">
  112. <title>Zend_Rest_Route with Zend_Config_Ini</title>
  113. <para>
  114. To use <classname>Zend_Rest_Route</classname> from an <acronym>INI</acronym> config file,
  115. use a route type parameter and set the config options:
  116. </para>
  117. <programlisting language="ini"><![CDATA[
  118. routes.rest.type = Zend_Rest_Route
  119. routes.rest.defaults.controller = object
  120. routes.rest.mod = project,user
  121. ]]></programlisting>
  122. <para>
  123. The 'type' option designates the RESTful routing config type. The 'defaults' option is used
  124. to specify custom default module, controller, and/or actions for the route. All other
  125. options in the config group are treated as RESTful module names, and their values are
  126. RESTful controller names. The example config defines
  127. <classname>Mod_ProjectController</classname> and <classname>Mod_UserController</classname>
  128. as RESTful controllers.
  129. </para>
  130. <para>
  131. Then use the <methodname>addConfig()</methodname> method of the Rewrite router object:
  132. </para>
  133. <programlisting language="php"><![CDATA[
  134. $config = new Zend_Config_Ini('path/to/routes.ini');
  135. $router = new Zend_Controller_Router_Rewrite();
  136. $router->addConfig($config, 'routes');
  137. ]]></programlisting>
  138. </sect4>
  139. <sect4 id="zend.rest.controller">
  140. <title>Zend_Rest_Controller</title>
  141. <para>
  142. To help or guide development of Controllers for use with
  143. <classname>Zend_Rest_Route</classname>, extend your Controllers from
  144. <classname>Zend_Rest_Controller</classname>.
  145. <classname>Zend_Rest_Controller</classname> defines the 5 most-commonly
  146. needed operations for RESTful resources in the form of abstract action
  147. methods.
  148. </para>
  149. <itemizedlist>
  150. <listitem>
  151. <para>
  152. <emphasis><methodname>indexAction()</methodname></emphasis> -
  153. Should retrieve an index of resources and assign it to view.
  154. </para>
  155. </listitem>
  156. <listitem>
  157. <para>
  158. <emphasis><methodname>getAction()</methodname></emphasis> -
  159. Should retrieve a single resource identified by <acronym>URI</acronym>
  160. and assign it to view.
  161. </para>
  162. </listitem>
  163. <listitem>
  164. <para>
  165. <emphasis><methodname>postAction()</methodname></emphasis> -
  166. Should accept a new single resource and persist its state.
  167. </para>
  168. </listitem>
  169. <listitem>
  170. <para>
  171. <emphasis><methodname>putAction()</methodname></emphasis> -
  172. Should accept a single resource idenitifed by <acronym>URI</acronym>
  173. and persist its state.
  174. </para>
  175. </listitem>
  176. <listitem>
  177. <para>
  178. <emphasis><methodname>deleteAction()</methodname></emphasis> -
  179. Should delete a single resource identified by <acronym>URI</acronym>.
  180. </para>
  181. </listitem>
  182. </itemizedlist>
  183. </sect4>
  184. </sect3>
  185. <!--
  186. vim:se ts=4 sw=4 et:
  187. -->