PSR-7 Standard – Part 6 – Server Requests
This post is part of series:
- Part 1: Overview
- Part 2: Request and URI
- Part 3: Response
- Part 4: File Uploads
- Part 5: HTTP-Client
- Part 6: Server Request
- Part 7: Middleware
- Part 8: Usage in a Magento module
In Part 3 we already discovered the RequestInterface
which is used on client side. In this part, we have a more detailed look on the server side.
The Server Request inherits all methods of the RequestInterface
and has 13 additional methods. Six methods are available to setup the request object:
- withCookieParams
- withQueryParams
- withUploadedFiles
- withParsedBody
- withAttribute
- withoutAttribute
Like on client side we have to be careful. The Server Request is designed to be immutable (see Part 3). If we look in the code of the Guzzle\Psr7 implementation of the Server Request, we can see that all this methods are needed to create complete request from all global variables
($_SERVER, $_COOKIE, $_GET, $_POST, $_FILES) which we will get from PHP.
/**
* Return a ServerRequest populated with superglobals:
* $_GET
* $_POST
* $_COOKIE
* $_FILES
* $_SERVER
*
* @return ServerRequestInterface
*/
public static function fromGlobals()
{
//.... skipped code
$serverRequest = new ServerRequest($method, $uri, $headers, $body, $protocol, $_SERVER);
return $serverRequest
->withCookieParams($_COOKIE)
->withQueryParams($_GET)
->withParsedBody($_POST)
->withUploadedFiles(self::normalizeFiles($_FILES));
}
(ServerRequestInterface on Github)
The other methods are to consume the same data:
- getServerParams
- getCookieParams
- getQueryParams
- getParsedBody
- getAttributes
- getAttribute
So we have “setter” and matching “getter” methods.
What’s up with the “getAttributes” and “getAttribute” methods? This methods are important for the next part of our blog series. The Middleware.
– Creator of n98-magerun
– Fan of football club @wormatia
– Magento user since version 0.8 beta
– 8x certified Magento developer
– PHP Top 1.000 developer (yes, I’m PHP4 certified and sooooo old)
– Chief development officer at netz98