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#
| Method | Description |
|---|---|
__construct | Creates a server request. |
__clone | Clones a parsed body that is an object, so a copy does not share it. |
getUri | Adds HTTPS detection from server params ('on' or '1') on top of Request::getUri. |
getServerParams | Retrieve server parameters. |
getCookieParams | Retrieve cookies. |
withCookieParams | Return an instance with the specified cookies. |
getQueryParams | Retrieve query string arguments. |
withQueryParams | Return an instance with the specified query string arguments. |
getUploadedFiles | Retrieve normalized file upload data. |
withUploadedFiles | Create a new instance with the specified uploaded files. |
getParsedBody | Retrieve any parameters provided in the request body. |
getBody | Gets the body of the message. |
withBody | Return an instance with the specified message body. |
cookies | The cookies of a Cookie header, as PHP fills $_COOKIE: the first of equal names wins, values are URL-decoded. |
withParsedBody | Return an instance with the specified body parameters. |
getAttributes | Retrieve attributes derived from the request. |
getAttribute | Retrieve a single derived request attribute. |
withAttribute | Returns a copy with the attribute $name set to $value, for what middleware derived from the request. |
withoutAttribute | Return an instance that removes the specified derived request attribute. |
Examples#
$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#
RequestA PSR-7 outgoing, client-side request.UploadedFileA PSR-7 file uploaded with a request.