Site icon dev98

PSR-7 Standard – Part 6 – Server Requests

This post is part of series:


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:

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:

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.

Exit mobile version