File: Zend_Controller-Router-Route-Rest.xml

package info (click to toggle)
zendframework 1.12.9%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 133,584 kB
  • sloc: xml: 1,311,829; php: 570,173; sh: 170; makefile: 125; sql: 121
file content (219 lines) | stat: -rw-r--r-- 8,489 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect3 id="zend.controller.router.routes.rest">
    <title>Zend_Rest_Route</title>

    <para>
        The <classname>Zend_Rest</classname> component contains a RESTful route
        for <classname>Zend_Controller_Router_Rewrite</classname>. This route
        offers a standardized routing scheme that routes requests by translating
        the <acronym>HTTP</acronym> method and the <acronym>URI</acronym>
        to a module, controller, and action. The table below provides an overview
        of how request methods and <acronym>URI</acronym>'s are routed.
    </para>

    <table frame="all">
        <title>Zend_Rest_Route Behavior</title>

        <tgroup cols='3' align='left' colsep='1' rowsep='1'>
            <colspec colname='method' />
            <colspec colname='URI' />
            <colspec colname='route' />

            <thead>
                <row>
                    <entry>Method</entry>
                    <entry><acronym>URI</acronym></entry>
                    <entry>Module_Controller::action</entry>
                </row>
            </thead>

            <tbody>
                <row>
                    <entry><constant>GET</constant></entry>
                    <entry><filename>/product/ratings/</filename></entry>
                    <entry><methodname>Product_RatingsController::indexAction()</methodname></entry>
                </row>

                <row>
                    <entry><constant>GET</constant></entry>
                    <entry><filename>/product/ratings/:id</filename></entry>
                    <entry><methodname>Product_RatingsController::getAction()</methodname></entry>
                </row>

                <row>
                    <entry><constant>POST</constant></entry>
                    <entry><filename>/product/ratings</filename></entry>
                    <entry><methodname>Product_RatingsController::postAction()</methodname></entry>
                </row>

                <row>
                    <entry><constant>PUT</constant></entry>
                    <entry><filename>/product/ratings/:id</filename></entry>
                    <entry><methodname>Product_RatingsController::putAction()</methodname></entry>
                </row>

                <row>
                    <entry><constant>DELETE</constant></entry>
                    <entry><filename>/product/ratings/:id</filename></entry>

                    <entry>
                        <methodname>Product_RatingsController::deleteAction()</methodname>
                    </entry>
                </row>

                <row>
                    <entry><constant>POST</constant></entry>
                    <entry><filename>/product/ratings/:id?_method=PUT</filename></entry>
                    <entry><methodname>Product_RatingsController::putAction()</methodname></entry>
                </row>

                <row>
                    <entry><constant>POST</constant></entry>
                    <entry><filename>/product/ratings/:id?_method=DELETE</filename></entry>

                    <entry>
                        <methodname>Product_RatingsController::deleteAction()</methodname>
                    </entry>
                </row>
            </tbody>
        </tgroup>
    </table>

    <sect4 id="zend.rest.route_usage">
        <title>Zend_Rest_Route Usage</title>

        <para>
            To enable <classname>Zend_Rest_Route</classname> for an entire
            application, construct it with no config params and add it as the
            default route on the front controller:
        </para>

        <programlisting language="php"><![CDATA[
$front     = Zend_Controller_Front::getInstance();
$restRoute = new Zend_Rest_Route($front);
$front->getRouter()->addRoute('default', $restRoute);
]]></programlisting>

        <note>
            <para>
                If <classname>Zend_Rest_Route</classname> cannot match a valid
                module, controller, or action, it will return <constant>FALSE</constant> and the
                router will attempt to match using the next route in the router.
            </para>
        </note>

        <para>
            To enable <classname>Zend_Rest_Route</classname> for specific modules,
            construct it with an array of module names as the 3rd constructor argument:
        </para>

        <programlisting language="php"><![CDATA[
$front     = Zend_Controller_Front::getInstance();
$restRoute = new Zend_Rest_Route($front, array(), array('product'));
$front->getRouter()->addRoute('rest', $restRoute);
]]></programlisting>

        <para>
            To enable <classname>Zend_Rest_Route</classname> for specific
            controllers, add an array of controller names as the value of each module array element.
        </para>

        <programlisting language="php"><![CDATA[
$front     = Zend_Controller_Front::getInstance();
$restRoute = new Zend_Rest_Route($front, array(), array(
    'product' => array('ratings')
));
$front->getRouter()->addRoute('rest', $restRoute);
]]></programlisting>
    </sect4>

    <sect4 id="zend.rest.route_config">
        <title>Zend_Rest_Route with Zend_Config_Ini</title>

        <para>
            To use <classname>Zend_Rest_Route</classname> from an <acronym>INI</acronym> config
            file, use a route type parameter and set the config options:
        </para>

        <programlisting language="ini"><![CDATA[
routes.rest.type = Zend_Rest_Route
routes.rest.defaults.controller = object
routes.rest.mod = project,user
]]></programlisting>

        <para>
            The 'type' option designates the RESTful routing config type. The 'defaults' option is
            used to specify custom default module, controller, and/or actions for the route. All
            other options in the config group are treated as RESTful module names, and their values
            are RESTful controller names. The example config defines
            <classname>Mod_ProjectController</classname> and
            <classname>Mod_UserController</classname> as RESTful controllers.
        </para>

        <para>
            Then use the <methodname>addConfig()</methodname> method of the Rewrite router object:
        </para>

        <programlisting language="php"><![CDATA[
$config = new Zend_Config_Ini('path/to/routes.ini');
$router = new Zend_Controller_Router_Rewrite();
$router->addConfig($config, 'routes');
]]></programlisting>
    </sect4>

    <sect4 id="zend.rest.controller">
        <title>Zend_Rest_Controller</title>

        <para>
            To help or guide development of Controllers for use with
            <classname>Zend_Rest_Route</classname>, extend your Controllers from
            <classname>Zend_Rest_Controller</classname>.
            <classname>Zend_Rest_Controller</classname> defines the 5 most-commonly
            needed operations for RESTful resources in the form of abstract action
            methods.
        </para>

        <itemizedlist>
            <listitem>
                <para>
                    <emphasis><methodname>indexAction()</methodname></emphasis> -
                    Should retrieve an index of resources and assign it to view.
                </para>
            </listitem>

            <listitem>
                <para>
                    <emphasis><methodname>getAction()</methodname></emphasis> -
                    Should retrieve a single resource identified by <acronym>URI</acronym>
                    and assign it to view.
                </para>
            </listitem>

            <listitem>
                <para>
                    <emphasis><methodname>postAction()</methodname></emphasis> -
                    Should accept a new single resource and persist its state.
                </para>
            </listitem>

            <listitem>
                <para>
                    <emphasis><methodname>putAction()</methodname></emphasis> -
                    Should accept a single resource idenitifed by <acronym>URI</acronym>
                    and persist its state.
                </para>
            </listitem>

            <listitem>
                <para>
                    <emphasis><methodname>deleteAction()</methodname></emphasis> -
                    Should delete a single resource identified by <acronym>URI</acronym>.
                </para>
            </listitem>
        </itemizedlist>
    </sect4>
</sect3>
<!--
vim:se ts=4 sw=4 et:
-->