You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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:
0 commit comments