Developer Blog

  • Blog
  • /
  • PHPixie 3.0 HTTP
By Dracony on 26 April 2015

The HTTP abstraction library implementing the PSR-7 standard is out. 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 now will 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.

Here is a quick demo:

1
2
3
//Initialization is prety straightforward
$slice = new \PHPixie\Slice();
$http = new \PHPixie\HTTP($slice);

Requests

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//Build a Request from globals ($_GET etc)
$request = $http->request();

//Or you can pass a PSR-7 ServerRequestInterface to wrap
$request = $http->request($serverRequest);

//$_GET
$query = $request->query();

//$_POST
$query = $request->data();

//Additional attributes,
//e.g. parameters from routing
$query = $request->attributes();

//$_GET['pixie']
$query->get('pixie');

//With default value
$query->get('pixie', 'Trixie');

//Throw an exception if field is not set
$query->getRequired('pixie');

//$_GET['user']['name'];
$query->get('user.name');

//Or like this
$userData = $query->slice('user');
$userData->get('name');

//In this case $userData
//is an instance of \PHPixie\Slice\Data
//totally unrelated to HTTP library
//so you can pass it around the system
//without coupling to HTTP

//Accessing $_SERVER
$request->server()->get('http_host');

//Get a header line
//If multiple values are present
//for the same header, they will be
//concatenated with ','
$request->headers()->get('host');
$request->headers()->getRequired('host');

//Get header values as array
$request->headers()->getLines('accept');

//Handling uploads
$uploadedFile = $request->uploads()->get('file');
$uploadedFile->move('/images/fairy.png');

//HTTP method
$uri = $request->method();

//Accessing Uri
$uri = $request->uri();
$path = $uri->getPath();

//Underlying ServerRequest
$serverRequest = $request->serverRequest();

Response
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
$responses = $http->responses();

//The simplest response
$response = $responses->string('hello world');

//JSON with headers that forbid caching
$responses->json(array('name' => 'Pixie'));

//Redirect
$responses->redirect('http://phpixie.com/');

//File streaming
$responses->streamFile('pixie.png');

//Download contetnts as a file
//useful for CSV, TXT types
$responses->download('name.txt', 'text/plain', 'Trixie');

//File download
$responses->downloadFile('pixie.png', 'image.png', 'images/fairy.png');

//Modifying the status code
$response->setStatus('404', 'Not Found');

//OR you can omit the phrase to use
//the default one for the provided code
$response->setStatus('404');

//Modifying headers
$response->headers->set('Content-Type', 'text/csv');

//Transforming into PSR-7 Response
$response->asResponseMessage();

//Outputting a response
$http->output($response);

Context
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Generate context for a particular request
$context = $http->context($request);

//And then it's pretty straightforward
$cookies = $context->cookies();
$session = $context->session();

$cookies->set('lang', 'en');
$session->getRequired('user_id');

//Remember to pass the context
//when outputting the Response
//or generation a PSR-7 response
$http->output($response, $context);
$response->asResponseMessage($context);

Also you can just use the PSR-7 implementations without PHPixie wrappers:

1
2
3
//Get the PSR-7 factory
$psr7 = $http->messages();
$serverRequest = $psr7->sapiServerRequest();

As all the other PHPixie libraries it is 100% unit tested and has a high codeclimate score (actually perfect score in this case)

You can find the project here: https://github.com/phpixie/http
Composer snipper:

1
2
3
4
5
"require": {
    "phpixie/http": "3.*@dev",
    "phpixie/slice": "3.*@dev",
    "psr/http-message": "1.0.*@dev"
},

One more thing
I haven’t decided on a shortcut method for getting data from the request, so I’d love to hear your opinions. Here are the two ideas I came up with:

1
2
3
4
5
6
7
8
9
10
11
12
13
//The original way
$request->query()->get('pixie', 'Trixie');

//Idea #1
$request->query('pixie', 'Trixie');
//If parameters are present in query() and data()
//forward the call to the ->get method()

//Idea #2
$request->query()->pixie;
//Rely on __get
//But this doesn't allow for
//default values to be specified

Which one do you prefer?

comments powered by Disqus