source: trunk/prototype/library/tonic/README.markdown @ 6528

Revision 6528, 6.8 KB checked in by gustavo, 12 years ago (diff)

Ticket #2766 - Merge do branch das novas funcionalidaes para o trunk

  • Property svn:executable set to *
Line 
1PHP library/framework for building Web apps while respecting the 5 principles
2of RESTful design.
3
4 * Give every "thing" an ID (aka URIs)
5 * Link things together (HATEOAS)
6 * Use standard methods (aka the standard interface)
7 * Resources with multiple representations (aka standard document formats)
8 * Communicate statelessly
9
10[See the Tonic site for more info](http://peej.github.com/tonic/).
11
12
13How it works
14============
15
16Everything is a resource, and a resource is defined as a PHP class. An annotation
17wires a URI (or a collection of URIs) to the resource, and methods that match
18the HTTP methods by name allow interaction with it.
19
20    /**
21     * This class defines an example resource that is wired into the URI /example
22     * @uri /example
23     */
24    class ExampleResource extends Resource { }
25
26The incoming HTTP request is turned into a list of negotiated URIs based on the
27accept request headers which can then be used to pick the best representation
28for the response.
29
30    /**
31     * This class defines an example resource that is wired into the URI /example
32     * @uri /example
33     */
34    class ExampleResource extends Resource {
35       
36        function get($request) {
37           
38            $response = new Response($request);
39           
40            $response->code = Response::OK;
41            $response->body = 'Example response';
42           
43            return $response;
44           
45        }
46     
47    }
48
49
50How to get started
51==================
52
53The best place to get started is to get the hello world example running on your
54system, to do this you will need a web server running PHP5.1+.
55
56Place all of the Tonic files into your PHP include path so that other scripts can
57find it. By default on Windows this will probably be in "c:\php\includes\tonic" or
58on Linux/Unix it will be "/usr/share/php/tonic"
59
60Copy "docroot/dispatch.php" into your servers document root and edit it so that the
61require_once statement paths point to the Tonic library and the examples.
62
63Finally you need to route all incoming requests to dispatch.php. How you do this
64depends on your web server. If you are using Apache, the simplest way is to copy
65the .htaccess file from "docroot/.htaccess" into your Apache document root.
66
67
68Features
69========
70
71
72Request URI
73-----------
74
75The URI that is processed for the request when you create the Tonic Request object
76is gather by default from the REQUEST_URI Apache variable. If you need to gather
77the URI from another $_SERVER variable or somewhere else then you can pass it into
78the Request objects constructor as a configuration option:
79
80    $request = new Request(array(
81        'uri' => $_SERVER['PATH_INFO']
82    ));
83
84
85Base URI
86--------
87
88If you want to put your Tonic dispatcher at a URL that isn't the root of a domain
89then you'll need to let the Request object know so that the @uri annotations ignore
90it:
91
92    $request = new Request(array(
93        'baseUri' => '/some/base/uri'
94    ));
95
96Don't put a trailing slash on the end.
97
98
99URI annotations
100---------------
101
102Resources are attached to their URL by their @uri annotation:
103
104    /**
105     * @uri /example
106     */
107    class ExampleResource extends Resource { }
108
109As well as a straight forward URI string, you can also use a regular expression
110so that a resource is tied to a range of URIs:
111
112    /**
113     * @uri /example/([a-z]+)
114     */
115    class ExampleResource extends Resource {
116        function get($request, $parameter) {
117            ...
118        }
119    }
120
121URL template and Rails route style @uri annotations are also supported:
122
123    /**
124     * @uri /users/{username}
125     */
126    class ExampleResource extends Resource {
127        function get($request, $username) {
128            ...
129        }
130    }
131   
132    /**
133     * @uri /users/:username
134     */
135    class ExampleResource extends Resource {
136        function get($request, $username) {
137            ...
138        }
139    }
140
141It is also possible for multiple resource to match the same URI, so you can
142prioritise which resource should be used by specifying a priority level as part
143of the annotation:
144
145    /**
146     * @uri /example/([a-z]+)
147     */
148    class ExampleResource extends Resource { }
149
150    /**
151     * @uri /example/apple 2
152     */
153    class ExampleResource extends Resource { }
154
155By postfixing the @uri annotation with a number, of all the matching resources,
156the one with the highest postfixed number will be used.
157
158
159Mimetypes
160---------
161
162To handle content negotiation via filename style extensions to URLs as well the
163HTTP Accept header, a mapping between extensions and mimetypes can be provided.
164By default this list contains a number of common mappings, if you need to add one
165or more of your own, pass them into the constructor as an array:
166
167    $request = new Request(array(
168        'mimetypes' => array(
169            'ogv' => 'video/ogg'
170        )
171    ));
172
173
174Mount points
175------------
176
177To make resources more portable, it is possible to "mount" them into your URL-space
178by providing a namespace name to URL-space mapping. Every resource within that
179namespace will in effect have the URL-space prefixed to their @uri annotation.
180
181    $request = new Request(array(
182        'mount' => array(
183            'namespaceName' => '/some/mounted/uri'
184        )
185    ));
186
187Again, don't put a trailing slash on the end, and if you aren't using PHP5.3 and
188namespaces, you can use the @namespace annotation.
189
190
191Response exceptions
192-------------------
193
194The Request object and Resource objects can throw ResponseExceptions when a problem
195occurs that the object does not want to handle and so relinquishes control back
196to the dispatcher.
197
198The ResponseException has its code value set to the HTTP response code of the problem
199and its message set to a human readable reason for throwing the exception. The
200ResponseException::response() method can be used to produce a default Response object
201expressing the exception if required.
202
203The Request object throws a 404 ResponseException when the resource to be loaded
204does not exist.
205
206The Resource object throws a 405 ResponseException when the HTTP method from the
207request is not able to be handled by the resource.
208
209If you don't want to handle a problem within your Resource class, you can throw your
210own ResponseException and handle it in the dispatcher. Look at the auth example for
211an example of how.
212
213
214Autoloading classes
215-------------------
216
217If you've got lots of resource classes and don't fancy including them all in your
218dispatcher, you can use the autoload function to load a resource class for a given
219URL-space.
220
221    $request = new Request(array(
222        'autoload' => array(
223            '/example/[a-z]+' => 'ClassName'
224        )
225    ));
226
227In this example, the class ClassName will be autoloaded via [the standard PHP
228__autoload method](http://php.net/manual/en/language.oop5.autoload.php).
229
230
231
232For more information, read the code. Start with the dispatcher "docroot/dispatch.php"
233and then the examples in the "examples" directory.
Note: See TracBrowser for help on using the repository browser.