1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect3 id="zend.controller.router.routes.rest">
4 <title>Zend_Rest_Route</title>
7 The <classname>Zend_Rest</classname> component contains a RESTful route
8 for <classname>Zend_Controller_Router_Rewrite</classname>. This route
9 offers a standardized routing scheme that routes requests by translating
10 the <acronym>HTTP</acronym> method and the <acronym>URI</acronym>
11 to a module, controller, and action. The table below provides an overview
12 of how request methods and <acronym>URI</acronym>'s are routed.
16 <title>Zend_Rest_Route Behavior</title>
18 <tgroup cols='3' align='left' colsep='1' rowsep='1'>
19 <colspec colname='method' />
20 <colspec colname='URI' />
21 <colspec colname='route' />
26 <entry><acronym>URI</acronym></entry>
27 <entry>Module_Controller::action</entry>
33 <entry><constant>GET</constant></entry>
34 <entry><filename>/product/ratings/</filename></entry>
35 <entry><methodname>Product_RatingsController::indexAction()</methodname></entry>
39 <entry><constant>GET</constant></entry>
40 <entry><filename>/product/ratings/:id</filename></entry>
41 <entry><methodname>Product_RatingsController::getAction()</methodname></entry>
45 <entry><constant>POST</constant></entry>
46 <entry><filename>/product/ratings</filename></entry>
47 <entry><methodname>Product_RatingsController::postAction()</methodname></entry>
51 <entry><constant>PUT</constant></entry>
52 <entry><filename>/product/ratings/:id</filename></entry>
53 <entry><methodname>Product_RatingsController::putAction()</methodname></entry>
57 <entry><constant>DELETE</constant></entry>
58 <entry><filename>/product/ratings/:id</filename></entry>
61 <methodname>Product_RatingsController::deleteAction()</methodname>
66 <entry><constant>POST</constant></entry>
67 <entry><filename>/product/ratings/:id?_method=PUT</filename></entry>
68 <entry><methodname>Product_RatingsController::putAction()</methodname></entry>
72 <entry><constant>POST</constant></entry>
73 <entry><filename>/product/ratings/:id?_method=DELETE</filename></entry>
76 <methodname>Product_RatingsController::deleteAction()</methodname>
83 <sect4 id="zend.rest.route_usage">
84 <title>Zend_Rest_Route Usage</title>
87 To enable <classname>Zend_Rest_Route</classname> for an entire
88 application, construct it with no config params and add it as the
89 default route on the front controller:
92 <programlisting language="php"><![CDATA[
93 $front = Zend_Controller_Front::getInstance();
94 $restRoute = new Zend_Rest_Route($front);
95 $front->getRouter()->addRoute('default', $restRoute);
100 If <classname>Zend_Rest_Route</classname> cannot match a valid
101 module, controller, or action, it will return <constant>FALSE</constant> and the
102 router will attempt to match using the next route in the router.
107 To enable <classname>Zend_Rest_Route</classname> for specific modules,
108 construct it with an array of module names as the 3rd constructor argument:
111 <programlisting language="php"><![CDATA[
112 $front = Zend_Controller_Front::getInstance();
113 $restRoute = new Zend_Rest_Route($front, array(), array('product'));
114 $front->getRouter()->addRoute('rest', $restRoute);
118 To enable <classname>Zend_Rest_Route</classname> for specific
119 controllers, add an array of controller names as the value of each module array element.
122 <programlisting language="php"><![CDATA[
123 $front = Zend_Controller_Front::getInstance();
124 $restRoute = new Zend_Rest_Route($front, array(), array(
125 'product' => array('ratings')
127 $front->getRouter()->addRoute('rest', $restRoute);
131 <sect4 id="zend.rest.route_config">
132 <title>Zend_Rest_Route with Zend_Config_Ini</title>
135 To use <classname>Zend_Rest_Route</classname> from an <acronym>INI</acronym> config
136 file, use a route type parameter and set the config options:
139 <programlisting language="ini"><![CDATA[
140 routes.rest.type = Zend_Rest_Route
141 routes.rest.defaults.controller = object
142 routes.rest.mod = project,user
146 The 'type' option designates the RESTful routing config type. The 'defaults' option is
147 used to specify custom default module, controller, and/or actions for the route. All
148 other options in the config group are treated as RESTful module names, and their values
149 are RESTful controller names. The example config defines
150 <classname>Mod_ProjectController</classname> and
151 <classname>Mod_UserController</classname> as RESTful controllers.
155 Then use the <methodname>addConfig()</methodname> method of the Rewrite router object:
158 <programlisting language="php"><![CDATA[
159 $config = new Zend_Config_Ini('path/to/routes.ini');
160 $router = new Zend_Controller_Router_Rewrite();
161 $router->addConfig($config, 'routes');
165 <sect4 id="zend.rest.controller">
166 <title>Zend_Rest_Controller</title>
169 To help or guide development of Controllers for use with
170 <classname>Zend_Rest_Route</classname>, extend your Controllers from
171 <classname>Zend_Rest_Controller</classname>.
172 <classname>Zend_Rest_Controller</classname> defines the 5 most-commonly
173 needed operations for RESTful resources in the form of abstract action
180 <emphasis><methodname>indexAction()</methodname></emphasis> -
181 Should retrieve an index of resources and assign it to view.
187 <emphasis><methodname>getAction()</methodname></emphasis> -
188 Should retrieve a single resource identified by <acronym>URI</acronym>
189 and assign it to view.
195 <emphasis><methodname>postAction()</methodname></emphasis> -
196 Should accept a new single resource and persist its state.
202 <emphasis><methodname>putAction()</methodname></emphasis> -
203 Should accept a single resource idenitifed by <acronym>URI</acronym>
204 and persist its state.
210 <emphasis><methodname>deleteAction()</methodname></emphasis> -
211 Should delete a single resource identified by <acronym>URI</acronym>.