Skip to content

Context

Jakob Tapuć edited this page May 18, 2021 · 6 revisions

Introduction

The Context class provides an easy DSL-like interface for accessing response and request objects. The setter methods are used for preparing a response to be sent to the client while most getters retrieve data from the request - including route params and wildcards. Additionally, context objects can validate different part of incoming data - body, forms and parameters.

Reference

attr(string $name): mixed

WIP

`bufferedBody(): Promise``

WIP

bufferedForm(): Promise

WIP

contentType(string $contentType): Context

WIP

cookie(string $name): ?RequestCookie

WIP

cookies(): array

WIP

getHttpServerRequest(): Request

WIP

getHttpServerResponse(): Response

WIP

halt(int $status = Status::OK, InputStream|string|null $stringOrStream = null, array $headers = []): void

WIP

hasAttr(string $name): bool

WIP

header(string $name, mixed $value): Context

WIP

host(): string

WIP

html(InputStream|string $stringOrStream): Context

WIP

json(mixed $data): Context

WIP

method(): string

WIP

port(): ?int

WIP

queryArray(): array

WIP

queryString(): string

WIP

redirect(string $uri, int $status = Status::FOUND): Context

Generates a redirect response. This function is different from halt in that doesn't stop processing the current handler coroutine.

$ctx->redirect('/foo', Status::FOUND);
$logger->info('This will be logged');

$ctx->halt(Status::NOT FOUND);
$logger->info('This will not be logged');

removeCookie(string $name): Context

Removes a cookie from the response.

removeHeader(string $name): Context

Removes a header from the response.

requestBody(): RequestBody

Gets the request body. The request body can be buffered.

requestHeader(string $name): ?string

Gets a request header.

response(InputStream|string $stringOrStream): Context

Sets the response body to the specified string or stream. Read more about streams here.

responseBody(): InputStream|string

Gets the response body. Useful when you need to append/modify the body inside filters or status/exception mappers. Note: in the future a BodyBuilder class might be introduced.

responseCookie(string $name, string $value = '', CookieAttributes $attributes = null): Context

Sets a response cookie.

session(): Session

Returns the session object associated with this request.

status(int $status, ?string $reason = null): Context

Sets the response status and reason.

streamedForm(): Iterator

WIP

userAgent(): ?string

Returns the user-agent string if it was set or null otherwise.

validatedBody(): Promise<ValidationContext>

Note: in the future the signature will look like:

validatedBody(string $validatorName): Promise<WrappedValue>`

WIP

validatedForm(): Promise<ValidationContext>

Note: in the future the signature will look like:

validatedForm(): Promise<WrappedForm>`

WIP

wildcards(): array

WIP