Skip to content

Commit e411261

Browse files
committed
added documentation
1 parent b269699 commit e411261

File tree

1 file changed

+164
-1
lines changed

1 file changed

+164
-1
lines changed

README.md

Lines changed: 164 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,167 @@ PHPixie HTTP library
1010
[![Author](http://img.shields.io/badge/author-@dracony-blue.svg?style=flat-square)](https://twitter.com/dracony)
1111
[![Source Code](http://img.shields.io/badge/source-phpixie/http-blue.svg?style=flat-square)](https://github.com/phpixie/http)
1212
[![Software License](https://img.shields.io/badge/license-BSD-brightgreen.svg?style=flat-square)](https://github.com/phpixie/http/blob/master/LICENSE)
13-
[![Total Downloads](https://img.shields.io/packagist/dt/phpixie/http.svg?style=flat-square)](https://packagist.org/packages/phpixie/http)
13+
[![Total Downloads](https://img.shields.io/packagist/dt/phpixie/http.svg?style=flat-square)](https://packagist.org/packages/phpixie/http)
14+
15+
This library handles HTTP protocol asbtraction and implements the PSR-7 implemenetation. In addition to implementing the standard it provides wrappers for Requests and Responses and also abstractions for Cookies and Session. Since these wrappers work with any PSR-7 implementation it will now be possible to run PHPixie in some interesting environments, like inside ReactPHP etc. You can also use these abstractions to create your own PSR-7 compatible microframework.
16+
17+
Here is a quick demo:
18+
19+
```php
20+
//Without the PHPixie Framework
21+
$slice = new \PHPixie\Slice();
22+
$http = new \PHPixie\HTTP($slice);
23+
24+
//inside the framework
25+
$http = $frameworkBuilder->components()->http();
26+
```
27+
28+
**Requests**
29+
30+
```php
31+
//Build a Request from globals ($_GET etc)
32+
$request = $http->request();
33+
34+
//Or you can pass a PSR-7 ServerRequestInterface to wrap
35+
$request = $http->request($serverRequest);
36+
37+
//$_GET
38+
$query = $request->query();
39+
40+
//$_POST
41+
$query = $request->data();
42+
43+
//Additional attributes,
44+
//e.g. parameters from routing
45+
$query = $request->attributes();
46+
47+
//$_GET['pixie']
48+
$query->get('pixie');
49+
50+
//With default value
51+
$query->get('pixie', 'Trixie');
52+
53+
//Throw an exception if field is not set
54+
$query->getRequired('pixie');
55+
56+
//$_GET['user']['name'];
57+
$query->get('user.name');
58+
59+
//Or like this
60+
$userData = $query->slice('user');
61+
$userData->get('name');
62+
63+
//In this case $userData
64+
//is an instance of \PHPixie\Slice\Data
65+
//totally unrelated to HTTP library
66+
//so you can pass it around the system
67+
//without coupling to HTTP
68+
69+
//Accessing $_SERVER
70+
$request->server()->get('http_host');
71+
72+
//Get a header line
73+
//If multiple values are present
74+
//for the same header, they will be
75+
//concatenated with ','
76+
$request->headers()->get('host');
77+
$request->headers()->getRequired('host');
78+
79+
//Get header values as array
80+
$request->headers()->getLines('accept');
81+
82+
//Handling uploads
83+
$uploadedFile = $request->uploads()->get('file');
84+
$uploadedFile->move('/images/fairy.png');
85+
86+
//HTTP method
87+
$uri = $request->method();
88+
89+
//Accessing Uri
90+
$uri = $request->uri();
91+
$path = $uri->getPath();
92+
93+
//Underlying ServerRequest
94+
$serverRequest = $request->serverRequest();
95+
```
96+
97+
**Response**
98+
Apart from provideing a Response wrapper, PHPixie HTTP also can simplify building some frequently used responses, like JSON responses with proper headers and file downloads.
99+
100+
```php
101+
$responses = $http->responses();
102+
103+
//The simplest response
104+
$response = $responses->string('hello world');
105+
106+
//JSON with headers that forbid caching
107+
$responses->json(array('name' => 'Pixie'));
108+
109+
//Redirect
110+
$responses->redirect('http://phpixie.com/');
111+
112+
//File streaming
113+
$responses->streamFile('pixie.png');
114+
115+
//Download contetnts as a file
116+
//useful for CSV, TXT types
117+
$responses->download('name.txt', 'text/plain', 'Trixie');
118+
119+
//File download
120+
$responses->downloadFile('pixie.png', 'image.png', 'images/fairy.png');
121+
122+
//Modifying the status code
123+
$response->setStatus('404', 'Not Found');
124+
125+
//OR you can omit the phrase to use
126+
//the default one for the provided code
127+
$response->setStatus('404');
128+
129+
//Modifying headers
130+
$response->headers->set('Content-Type', 'text/csv');
131+
132+
//Transforming into PSR-7 Response
133+
$response->asResponseMessage();
134+
135+
//Outputting a response
136+
$http->output($response);
137+
```
138+
139+
**Context**
140+
Another important part is managing users Cookies and Session. Frequently you would access them outside of the request processing, for example in you authroization library. Also for non0standard environments, like in ReactPHP you would need special handlers for modifying the session. That’s why PHPixie separates these into a Context instance. You can store it separately, allowing users to modify cookies independently and then merge them with the Response. It’s rather easy:
141+
142+
```php
143+
//Generate context for a particular request
144+
$context = $http->context($request);
145+
146+
//And then it's pretty straightforward
147+
$cookies = $context->cookies();
148+
$session = $context->session();
149+
150+
$cookies->set('lang', 'en');
151+
$session->getRequired('user_id');
152+
153+
//Remember to pass the context
154+
//when outputting the Response
155+
//or generation a PSR-7 response
156+
$http->output($response, $context);
157+
$response->asResponseMessage($context);
158+
```
159+
160+
To obtain the HTTP Context when using the PHPixie framework, you need to get it from the framework context:
161+
162+
```php
163+
$httpContext = $frameworkBuilder->context()->httpContext();
164+
$cookies = $httpContext->cookies();
165+
$session = $httpContext->session();
166+
```
167+
168+
Also you can just use the PSR-7 implementations without PHPixie wrappers:
169+
170+
```php
171+
//Get the PSR-7 factory
172+
$psr7 = $http->messages();
173+
$serverRequest = $psr7->sapiServerRequest();
174+
```
175+
176+
As all the other PHPixie libraries it is 100% unit tested and has a high codeclimate score (actually perfect score in this case)

0 commit comments

Comments
 (0)