7
7
The ** icanboogie/http** package provides a foundation to handle HTTP requests, with representations
8
8
for requests, request files, responses, and headers. The package also lay the foundation of
9
9
10
- The following example is an overview of a request processing:
10
+ The following example is an overview of request processing:
11
11
12
12
``` php
13
13
<?php
@@ -78,8 +78,8 @@ $request = Request::from([
78
78
79
79
### Safe and idempotent requests
80
80
81
- Safe methods are HTTP methods that do not modify resources. For instance, using ` GET ` or ` HEAD ` on a
82
- resource URL, should NEVER change the resource.
81
+ Safe methods are HTTP methods that don't modify resources.
82
+ For instance, using ` GET ` or ` HEAD ` on a resource URL, should NEVER change the resource.
83
83
84
84
The ` is_safe ` property may be used to check if a request is safe or not.
85
85
@@ -93,9 +93,9 @@ Request::from([ Request::OPTION_METHOD => Request::METHOD_POST ])->is_safe; // f
93
93
Request::from([ Request::OPTION_METHOD => Request::METHOD_DELETE ])->is_safe; // false
94
94
```
95
95
96
- An idempotent HTTP method is a HTTP method that can be called many times without different outcomes.
97
- It would not matter if the method is called only once, or ten times over. The result should be the
98
- same.
96
+ An idempotent HTTP method is an HTTP method that can be called many times without different
97
+ outcomes. It would not matter if the method is called only once, or ten times over. The result
98
+ should be the same.
99
99
100
100
The ` is_idempotent ` property may be used to check if a request is idempotent or not.
101
101
@@ -136,7 +136,7 @@ $request = Request::from($_SERVER)->with([
136
136
137
137
### Request parameters
138
138
139
- Whether they are sent as part of the query string, the post body, or the path info, parameters sent
139
+ Whether they're sent as part of the query string, the POST body, or the path info, parameters sent
140
140
along a request are collected in arrays. The ` query_params ` , ` request_params ` , and ` path_params `
141
141
properties give you access to these parameters.
142
142
@@ -237,9 +237,8 @@ $file = $files['uploaded']; // instanceof File
237
237
$file = $files['undefined']; // null
238
238
```
239
239
240
- Uploaded files, and _ pretend_ uploaded files, are represented by [ File] [ ] instances. The class
241
- tries its best to provide the same API for both. The ` is_uploaded ` property helps you set
242
- them apart.
240
+ [ File] [ ] represents uploaded files, and _ pretend_ uploaded files, with a single API.
241
+ The ` is_uploaded ` property helps you set them apart.
243
242
244
243
The ` is_valid ` property is a simple way to check if a file is valid. The ` move() ` method
245
244
lets you move the file out of the temporary folder or around the filesystem.
@@ -282,7 +281,7 @@ echo $file->match('image'); // false
282
281
echo $file->match('.png'); // false
283
282
```
284
283
285
- The method also handles sets, and returns ` true ` if there's any match:
284
+ The method also handles sets, and returns ` true ` if there is any match:
286
285
287
286
``` php
288
287
<?php
@@ -319,9 +318,9 @@ $file->to_array();
319
318
320
319
### Request context
321
320
322
- Because requests may be nested the request context offers a safe place where you can store the state
323
- of your application that is relative to a request, for instance a request relative site, page,
324
- route, dispatcher… The context may be used as an array, but is also a prototyped instance.
321
+ Because requests may be nested, the request context offers a safe place where you can store the
322
+ state of your application that is relative to a request. For instance, the context can store the
323
+ relative site, page, route, dispatcher…
325
324
326
325
The following example demonstrates how to store a value in a request context:
327
326
@@ -330,32 +329,16 @@ The following example demonstrates how to store a value in a request context:
330
329
331
330
namespace ICanBoogie\HTTP;
332
331
333
- $request = Request::from($_SERVER);
334
- $request->context['site'] = $app->models['sites']->one;
335
- ```
336
-
337
- The following example demonstrates how to use the prototype feature to provide a value when it is
338
- requested from the context:
332
+ /** @var Request $request */
333
+ /** @var \ICanBoogie\Routing\Route $route */
339
334
340
- ``` php
341
- <?php
335
+ $request->context->add($route);
342
336
343
- namespace ICanBoogie\HTTP;
344
-
345
- use ICanBoogie\HTTP\Request\Context;
346
- use ICanBoogie\Prototype;
347
-
348
- Prototype::from(Context::class)['lazy_get_site'] = function(Context $context) use ($site_model) {
349
-
350
- return $site_model->resolve_from_request($context->request);
351
-
352
- };
337
+ // …
353
338
354
- $request = Request::from($_SERVER);
355
-
356
- $site = $request->context['site'];
357
- # or
358
- $site = $request->context->site;
339
+ $route = $request->conntext->find(Route::class);
340
+ # or, if the value is required
341
+ $route = $request->conntext->get(Route::class);
359
342
```
360
343
361
344
@@ -364,23 +347,25 @@ $site = $request->context->site;
364
347
365
348
### Obtaining a response
366
349
367
- A response is obtained from a request simply by invoking the request, or by invoking one of the
368
- available HTTP methods. The ` dispatch() ` helper is used to dispatch the request. A [ Response] [ ]
369
- instance is returned if the dispatching is successful, a [ NotFound] [ ] exception is
370
- thrown otherwise.
350
+ A response is the result of a [ Responder] [ ] 's ` respond() ` method. An exception is thrown when a
351
+ response can't be provided; for example, [ NotFound] [ ] .
371
352
372
353
``` php
373
354
<?php
374
355
375
356
namespace ICanBoogie\HTTP;
376
357
377
358
/* @var $request Request */
359
+ /* @var ResponderProvider $responder_provider */
378
360
379
- $response = $request();
361
+ // The Responder Provider matches a request with a Responder
362
+ $responder = $responder_provider->responder_for_request($request);
380
363
381
- # using the POST method and additional parameters
364
+ // The Responder responds to the request with a Response, it might also throw an exception.
365
+ $response = $responder->respond($request);
382
366
383
- $response = $request->post([ 'param' => 'value' ]);
367
+ // The response is sent to the client.
368
+ $response();
384
369
```
385
370
386
371
@@ -425,7 +410,7 @@ $response();
425
410
426
411
### Response status
427
412
428
- The response status is represented by a [ Status] [ ] instance. It may be defined as a HTTP response
413
+ The response status is represented by a [ Status] [ ] instance. It may be defined as an HTTP response
429
414
code such as ` 200 ` , an array such as ` [ 200, "Ok" ] ` , or a string such as ` "200 Ok" ` .
430
415
431
416
``` php
@@ -454,7 +439,7 @@ $response->status->is_not_found; // true
454
439
455
440
### Streaming the response body
456
441
457
- When a large response body needs to be streamed, it is recommended to use a closure as response
442
+ When a large response body needs to be streamed, it is recommended to use a closure as a response
458
443
body instead of a huge string that would consume a lot of memory.
459
444
460
445
``` php
@@ -488,10 +473,10 @@ $response = new Response($output, Response::STATUS_OK, [ 'Content-Type' => 'text
488
473
489
474
Before v2.3.2 the ` Content-Length ` header field was added automatically when it was computable,
490
475
for instance when the body was a string or an instance implementing ` __toString() ` .
491
- Starting v2.3.2 this is no longer the case and the header field has to be defined when required.
476
+ Starting v2.3.2, this is no longer the case, and the header field has to be defined when required.
492
477
This was decided to prevent a bug with Apache+FastCGI+DEFLATE where the ` Content-Length ` field
493
- was not adjusted although the body was compressed. Also, in most cases it's not such a good idea
494
- to define that field for generated content because it prevents the response to be sent as
478
+ wasn't adjusted although the body was compressed. Also, in most cases it is not such a good idea
479
+ to define that field for generated content because it prevents the response from being sent as
495
480
[ compressed chunks] ( http://en.wikipedia.org/wiki/Chunked_transfer_encoding ) .
496
481
497
482
@@ -519,7 +504,7 @@ $response->status->is_redirect; // true
519
504
### Delivering a file
520
505
521
506
A file may be delivered using a [ FileResponse] [ ] instance. Cache control and _ range_ requests
522
- are handled automatically, you just need to provide the pathname of the file, or a ` SplFileInfo `
507
+ are handled automatically; you only have to provide the pathname of the file, or a ` SplFileInfo `
523
508
instance, and a request.
524
509
525
510
``` php
@@ -533,7 +518,7 @@ $response = new FileResponse("/absolute/path/to/my/file", $request);
533
518
$response();
534
519
```
535
520
536
- The ` OPTION_FILENAME ` option may be used to force downloading. Of course, utf-8 string are
521
+ The ` OPTION_FILENAME ` option may be used to force downloading. Of course, utf-8 strings are
537
522
supported:
538
523
539
524
``` php
@@ -554,11 +539,11 @@ $response();
554
539
555
540
The following options are also available:
556
541
557
- - ` OPTION_ETAG ` : Specifies the ` ETag ` header field of the response. If it is not defined the
542
+ - ` OPTION_ETAG ` : Specifies the ` ETag ` header field of the response. If it is not defined, the
558
543
[ SHA-384] [ ] of the file is used instead.
559
544
560
545
- ` OPTION_EXPIRES ` : Specifies the expiration date as a ` DateTime ` instance or a relative date
561
- such as " +3 month" , which maps to the ` Expires ` header field. The ` max-age ` directive of the
546
+ such as ` +3 month ` , which maps to the ` Expires ` header field. The ` max-age ` directive of the
562
547
` Cache-Control ` header field is computed from the current time. If it is not defined
563
548
` DEFAULT_EXPIRES ` is used instead ("+1 month").
564
549
@@ -569,7 +554,7 @@ The following properties are available:
569
554
570
555
- ` modified_time ` : Returns the last modified timestamp of the file.
571
556
572
- - ` is_modified ` : Whether the file was modified since the last response. The value is computed
557
+ - ` is_modified ` : Whether the file has been modified since the last response. The value is computed
573
558
using the request header fields ` If-None-Match ` and ` If-Modified-Since ` , and the properties
574
559
` modified_time ` and ` etag ` .
575
560
@@ -579,7 +564,8 @@ using the request header fields `If-None-Match` and `If-Modified-Since`, and the
579
564
580
565
## Headers
581
566
582
- Here's an overview of headers usage, details are available in the [ Headers documentation] ( docs/Headers.md ) .
567
+ Here is an overview of header usage.
568
+ Details are available in the [ Headers documentation] ( docs/Headers.md ) .
583
569
584
570
``` php
585
571
<?php
@@ -627,22 +613,22 @@ echo $headers['X-My-Header']; // 'Some value';
627
613
628
614
## Exceptions
629
615
630
- The following exceptions are defined by the HTTP package :
616
+ The HTTP package defines the following exceptions :
631
617
632
618
* [ ClientError] [ ] : thrown when a client error occurs.
633
619
* [ NotFound] [ ] : thrown when a resource is not found. For instance, this exception is
634
620
thrown by the dispatcher when it fails to resolve a request into a response.
635
621
* [ AuthenticationRequired] [ ] : thrown when the authentication of the client is required. Implements [ SecurityError] [ ] .
636
622
* [ PermissionRequired] [ ] : thrown when the client lacks a required permission. Implements [ SecurityError] [ ] .
637
- * [ MethodNotAllowed] [ ] : thrown when a HTTP method is not supported.
623
+ * [ MethodNotAllowed] [ ] : thrown when an HTTP method is not supported.
638
624
* [ ServerError] [ ] : throw when a server error occurs.
639
625
* [ ServiceUnavailable] [ ] : thrown when a server is currently unavailable
640
626
(because it is overloaded or down for maintenance).
641
627
* [ ForceRedirect] [ ] : thrown when a redirect is absolutely required.
642
- * [ StatusCodeNotValid] [ ] : thrown when a HTTP status code is not valid.
628
+ * [ StatusCodeNotValid] [ ] : thrown when an HTTP status code is not valid.
643
629
644
630
Exceptions defined by the package implement the ` ICanBoogie\HTTP\Exception ` interface.
645
- Using this interface one can easily catch HTTP related exceptions:
631
+ Using this interface, one can easily catch HTTP- related exceptions:
646
632
647
633
``` php
648
634
<?php
@@ -669,10 +655,6 @@ catch (\Exception $e)
669
655
670
656
The following helpers are available:
671
657
672
- * [ ` dispatch() ` ] [ ] : Dispatches a request using the dispatcher returned by [ ` get_dispatcher() ` ] [ ] .
673
- * [ ` get_dispatcher() ` ] [ ] : Returns the request dispatcher. If no dispatcher provider is defined,
674
- the method defines a new instance of [ DispatcherProvider] [ ] as provider and use it to retrieve the
675
- dispatcher.
676
658
* [ ` get_initial_request() ` ] [ ] : Returns the initial request.
677
659
678
660
``` php
@@ -737,6 +719,7 @@ See [CONTRIBUTING](CONTRIBUTING.md) for details.
737
719
[ PermissionRequired ] : lib/PermissionRequired.php
738
720
[ RedirectResponse ] : lib/RedirectResponse.php
739
721
[ Request ] : lib/Request.php
722
+ [ Responder ] : lib/Responder.php
740
723
[ Response ] : lib/Response.php
741
724
[ SecurityError ] : lib/SecurityError.php
742
725
[ ServerError ] : lib/ServerError.php
0 commit comments