Zend_Controller-Router-Route-Rest.xml 8.3 KB

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