📜 ⬆️ ⬇️

A modern approach to HTTP with PHPixie and PSR-7

image
Standard PHP API for working with HTTP requests is outdated. Programmers have learned not to use global variables, but standard superglobes like $ _GET, $ _SERVER still remind us of the distant past. Of course, frameworks encapsulate this information into their Request \ Response classes, but there are a lot of such implementations and there is not yet a single standard. The PSR-7 standard from PHP-FIG should just bring the representation of the HTTP protocol to a single denominator that allows writing Middleware which will work on many frameworks at once. It has not yet been adopted, but the early voting showed almost unanimous support for the new standard. PHPixie preparing for release of version 3.0 has already adopted and implemented PSR-7, and also provides wrappers for simplified work with the interface. If you want to create your own microfilm by taking PHPixie HTTP as a basis, you can achieve results in one evening.

Now let's look at the implementation itself:

$slice = new \PHPixie\Slice(); $http = new \PHPixie\HTTP($slice); 


Request
PSR-7 is a rather simplified interface and provides $ _GET and $ _POST parameters in the form of arrays, as well as PHP itself; PHPixie wrappers greatly simplify working with them:
')
 //     $request = $http->request(); //      // PSR-7 ServerRequestInterface $request = $http->request($serverRequest); //$_GET $query = $request->query(); //$_POST $query = $request->data(); //  //    $query = $request->attributes(); //$_GET['pixie'] $query->get('pixie'); //   $query->get('pixie', 'Trixie'); //     $query->getRequired('pixie'); //$_GET['user']['name']; $query->get('user.name'); //  $userData = $query->slice('user'); $userData->get('name'); //    $userData //  \PHPixie\Slice\Data //    HTTP //     -  //     $request->server()->get('http_host'); //      $request->headers()->get('host'); $request->headers()->getRequired('host'); //    $request->headers()->getLines('accept'); //    PSR-7 $uploadedFile = $request->uploads()->get('file'); $uploadedFile->move('/images/fairy.png'); //  URI  $uri = $request->uri(); $path = $uri->getPath(); // -    // ServerRequestInterface $serverRequest = $request->serverRequest(); 


Response
In addition to the wrapper itself over HTTP responses, PHPixie will also be able to build frequently used responses automatically to save you from messing with headers. Of course, after the answer is built it can be modified to taste.

 $responses = $http->responses(); //   $response = $responses->string('hello world'); //JSON       $responses->json(array('name' => 'Pixie')); // $responses->redirect('http://phpixie.com/'); //  $responses->streamFile('pixie.png'); //       //  CSV, TXT $responses->download('name.txt', 'text/plain', 'Trixie'); //   $responses->downloadFile('pixie.png', 'image.png', 'images/fairy.png'); //  $response->setStatus('404', 'Not Found'); //     $response->setStatus('404'); //  $response->headers->set('Content-Type', 'text/csv'); // PSR-7 ResponseInterface $response->asResponseMessage(); //  $http->output($response); 


Context
It would seem that this is all, but we missed cookies and session. They refer both to the request and to the response and often they need access not only in the controller, but also in other places, for example in the authorization module. PHPixie highlights them in a separate Context.

 //      $context = $http->context($request); //       $cookies = $context->cookies(); $session = $context->session(); $cookies->set('lang', 'en'); $session->getRequired('user_id'); //     //   $http->output($response, $context); $response->asResponseMessage($context); 


There are other implementations of PSR-7, but so far they are without wrappers and context, which makes working with them rather inconvenient. Quite a large number of them use traits and thus need the PHP 5.4+ version.

At that time, all PHPixie libraries work under any version of PHP older than 5.3 (including the new 7 and HHVM) and, in addition, are 100% covered with unit tests. The code itself can be found at github.com/phpixie/http

Source: https://habr.com/ru/post/256639/


All Articles