@@ -80,7 +80,7 @@ $router->get('/profile/{username}', function ($req, $res) {
8080});
8181```
8282
83- You may also choose to greedily grab blocks of the path for manual parsing.
83+ You may also choose to greedily match blocks of the path for manual parsing.
8484For instance, if we wanted to distinguish between a ` json ` extension to a
8585URI agnostic of the inner match, we can do something like this:
8686
@@ -92,3 +92,117 @@ $router->get('/api/{path*}/json', function ($req, $res) {
9292});
9393```
9494
95+ This type of matching may be useful for doing a passthru to request static
96+ files not managed by Apache or Nginx configs:
97+
98+ ``` php
99+ // route all image requests to a handler
100+ $router->get('/img/{image*}', function ($req, $res) {
101+ ImageManager::serve($req->image);
102+ });
103+ ```
104+ ### Request and response objects
105+
106+ Each route handler will be passed two parameters: a request and a response
107+ object. These objects are able to be used to get detailed information and
108+ calculate the appropriate response. In most cases, a route handler should
109+ end with a call to ` $res->send() ` .
110+
111+ ## Request Object
112+
113+ When a router is initialized, a new ` Http\Request ` object is created to be
114+ passed into the handler. This object contains a lot of pre-parsed attributes
115+ to help manage your response.
116+
117+ ### HTTP request type
118+
119+ The HTTP method (or verb) will be assigned to the request object and can be
120+ accessed by property:
121+
122+ ``` php
123+ echo $req->method;
124+ ```
125+
126+ Valid supported types are ` GET ` , ` POST ` , ` PUT ` and ` DELETE ` .
127+
128+ ### HTTP request headers
129+
130+ All request headers are accessible on the request object as well. Rather than
131+ using the native PHP ` getallheaders() ` method, we are using a custom polyfill
132+ to allow retrieval of the headers in both Apache and Nginx environments.
133+
134+ Some request headers will be used by the router to set default values on the
135+ response object. To access the headers, you may reference the ` headers ` property
136+ of the request object:
137+
138+ ``` php
139+ $router->post('/api/user', function ($req, $res) {
140+ if ($req->headers['Content-Type'] == 'application/json') {
141+ // request has JSON payload
142+ }
143+ });
144+ ```
145+
146+ ### Query string
147+
148+ The request object will pre-parse the query string and assign it to the ` query `
149+ property. If you are doing logic based on expected query parameters, use this
150+ property.
151+
152+ ``` php
153+ $router->post('/calendar', function ($req, $res) {
154+ if (!isset($req->query['month'])) {
155+ $res->send('Please select a month!');
156+ } else {
157+ $res->send(Calendar::render($req->query['month']));
158+ }
159+ });
160+ ```
161+
162+ ### Request URI
163+
164+ The fully matched URI is assigned to the ` uri ` property of the request object.
165+
166+ ``` php
167+ $router->get('/profile/{username}', function ($req, $res) {
168+ $res->send($req->uri);
169+ });
170+ ```
171+
172+ In this example, if you then navigate to http://yourdomain.com/profile/guahanweb ,
173+ you would get a response of ` /profile/guahanweb ` .
174+
175+ ### Request body (data payload)
176+
177+ Parsing the request body is somethig we have tried to optimize a little. Rather
178+ than always parsing the payload, we will statically parse it * only when it has
179+ been requested the first time* . In other words, the body will not be parsed until
180+ the first time the ` body ` attribute is referenced. If you reference the ` body `
181+ property again, the payload will not be parsed a second time.
182+
183+ Additionally, there are a few assumptions that are made about the body based on
184+ the combination of HTTP verb and Content-Type header.
185+
186+ #### GET and DELETE
187+
188+ According to the HTTP/1.1 spec, ` GET ` and ` DELETE ` request bodies, if present,
189+ should not have any meaning. Therefore, the request object will short circuit
190+ any body parsing with a ` null ` value on these verbs.
191+
192+ #### PUT
193+
194+ The body for a ` PUT ` request will be read from ` STDIN ` . If the Content-Type of
195+ the request is ` application/json ` , the body will also be parsed for JSON content,
196+ and the resulting object will be returned.
197+
198+ #### POST
199+
200+ The ` POST ` verb acts just like ` PUT ` with one additional caveat. In addition to
201+ a JSON payload, the ` POST ` verb can also take a Content-Type beginning with
202+ ` mulipart/form-data ` . In this case, since PHP has already processed the payload
203+ into the ` $_POST ` variable, the body will simply return that variable.
204+
205+ In both ` PUT ` and ` POST ` cases, if neither JSON nor form content are specified,
206+ the raw string of the body will be returned.
207+
208+ ## Response Object
0 commit comments