phasync
HTTP client / ServerRequest

Class

ServerRequest

A PSR-7 request as a server received it.

Introduction#

Adds the server and cookie parameters, the query parameters, the uploaded files, the parsed body and the attributes to Request. Like all PSR-7 messages it is immutable. The body, the uploaded files and the parsed body can be given as closures, so that a server parses them only when the application asks.

Class synopsis#

class ServerRequest extends Request implements RequestInterface, MessageInterface, ServerRequestInterface {
    /* Methods */
    public ServerRequest::__construct(
        string $method,
        string $requestTarget,
        StreamInterface|Closure|null $body,
        array $headers = [],
        ?array $queryParams = null,
        array $serverParams = [],
        array $cookieParams = [],
        Closure|array $uploadedFiles = [],
        mixed $parsedBody = null,
        array $attributes = [],
        string $protocolVersion = '1.1'
    )
    public ServerRequest::__clone()
    public ServerRequest::getUri(): UriInterface
    public ServerRequest::getServerParams(): array
    public ServerRequest::getCookieParams(): array
    public ServerRequest::withCookieParams($cookies): ServerRequestInterface
    public ServerRequest::getQueryParams(): array
    public ServerRequest::withQueryParams($query): ServerRequestInterface
    public ServerRequest::getUploadedFiles(): array
    public ServerRequest::withUploadedFiles($uploadedFiles): ServerRequestInterface
    public ServerRequest::getParsedBody()
    public ServerRequest::getBody(): StreamInterface
    public ServerRequest::withBody($body): MessageInterface
    public static ServerRequest::cookies(string $header): array
    public ServerRequest::withParsedBody($data): ServerRequestInterface
    public ServerRequest::getAttributes(): array
    public ServerRequest::getAttribute($name, $default = null)
    public ServerRequest::withAttribute($name, $value): ServerRequestInterface
    public ServerRequest::withoutAttribute($name): ServerRequestInterface
}

Methods#

MethodDescription
__constructCreates a server request.
__cloneClones a parsed body that is an object, so a copy does not share it.
getUriAdds HTTPS detection from server params ('on' or '1') on top of Request::getUri.
getServerParamsRetrieve server parameters.
getCookieParamsRetrieve cookies.
withCookieParamsReturn an instance with the specified cookies.
getQueryParamsRetrieve query string arguments.
withQueryParamsReturn an instance with the specified query string arguments.
getUploadedFilesRetrieve normalized file upload data.
withUploadedFilesCreate a new instance with the specified uploaded files.
getParsedBodyRetrieve any parameters provided in the request body.
getBodyGets the body of the message.
withBodyReturn an instance with the specified message body.
cookiesThe cookies of a Cookie header, as PHP fills $_COOKIE: the first of equal names wins, values are URL-decoded.
withParsedBodyReturn an instance with the specified body parameters.
getAttributesRetrieve attributes derived from the request.
getAttributeRetrieve a single derived request attribute.
withAttributeReturns a copy with the attribute $name set to $value, for what middleware derived from the request.
withoutAttributeReturn an instance that removes the specified derived request attribute.

Examples#

Example 1
$request = new ServerRequest('POST', '/login?next=/home', new StringStream('user=ann'), ['Host' => 'example.com'], parsedBody: ['user' => 'ann']);
$request->getQueryParams();   // ['next' => '/home']
$request->getParsedBody();    // ['user' => 'ann']

See also#