Documentation

Middleware
in package

Functions used to create and wrap handlers with handler middleware.

Table of Contents

cookies()  : callable
Middleware that adds cookies to requests.
history()  : callable
Middleware that pushes history data to an ArrayAccess container.
httpErrors()  : callable
Middleware that throws exceptions for 4xx or 5xx responses when the "http_errors" request option is set to true.
log()  : callable
Middleware that logs requests, responses, and errors using a message formatter.
mapRequest()  : callable
Middleware that applies a map function to the request before passing to the next handler.
mapResponse()  : callable
Middleware that applies a map function to the resolved promise's response.
prepareBody()  : callable
This middleware adds a default content-type if possible, a default content-length or transfer-encoding header, and the expect header.
redirect()  : callable
Middleware that handles request redirects.
retry()  : callable
Middleware that retries requests based on the boolean result of invoking the provided "decider" function.
tap()  : callable
Middleware that invokes a callback before and after sending a request.

Methods

cookies()

Middleware that adds cookies to requests.

public static cookies() : callable

The options array must be set to a CookieJarInterface in order to use cookies. This is typically handled for you by a client.

Return values
callable

Returns a function that accepts the next handler.

history()

Middleware that pushes history data to an ArrayAccess container.

public static history(array<string|int, mixed>|ArrayAccess<int, array<string|int, mixed>> &$container) : callable
Parameters
$container : array<string|int, mixed>|ArrayAccess<int, array<string|int, mixed>>

Container to hold the history (by reference).

Tags
throws
InvalidArgumentException

if container is not an array or ArrayAccess.

Return values
callable

httpErrors()

Middleware that throws exceptions for 4xx or 5xx responses when the "http_errors" request option is set to true.

public static httpErrors([BodySummarizerInterface|null $bodySummarizer = null ]) : callable
Parameters
$bodySummarizer : BodySummarizerInterface|null = null

The body summarizer to use in exception messages.

Return values
callable

log()

Middleware that logs requests, responses, and errors using a message formatter.

public static log(LoggerInterface $logger, MessageFormatterInterface|MessageFormatter $formatter[, string $logLevel = 'info' ]) : callable
Parameters
$logger : LoggerInterface

Logs messages.

$formatter : MessageFormatterInterface|MessageFormatter

Formatter used to create message strings.

$logLevel : string = 'info'

Level at which to log requests.

Tags
phpstan-param

\Psr\Log\LogLevel::* $logLevel Level at which to log requests.

Return values
callable

Returns a function that accepts the next handler.

mapRequest()

Middleware that applies a map function to the request before passing to the next handler.

public static mapRequest(callable $fn) : callable
Parameters
$fn : callable

Function that accepts a RequestInterface and returns a RequestInterface.

Return values
callable

mapResponse()

Middleware that applies a map function to the resolved promise's response.

public static mapResponse(callable $fn) : callable
Parameters
$fn : callable

Function that accepts a ResponseInterface and returns a ResponseInterface.

Return values
callable

prepareBody()

This middleware adds a default content-type if possible, a default content-length or transfer-encoding header, and the expect header.

public static prepareBody() : callable
Return values
callable

redirect()

Middleware that handles request redirects.

public static redirect() : callable
Return values
callable

Returns a function that accepts the next handler.

retry()

Middleware that retries requests based on the boolean result of invoking the provided "decider" function.

public static retry(callable $decider[, callable $delay = null ]) : callable

If no delay function is provided, a simple implementation of exponential backoff will be utilized.

Parameters
$decider : callable

Function that accepts the number of retries, a request, [response], and [exception] and returns true if the request is to be retried.

$delay : callable = null

Function that accepts the number of retries and returns the number of milliseconds to delay.

Return values
callable

Returns a function that accepts the next handler.

tap()

Middleware that invokes a callback before and after sending a request.

public static tap([callable $before = null ][, callable $after = null ]) : callable

The provided listener cannot modify or alter the response. It simply "taps" into the chain to be notified before returning the promise. The before listener accepts a request and options array, and the after listener accepts a request, options array, and response promise.

Parameters
$before : callable = null

Function to invoke before forwarding the request.

$after : callable = null

Function invoked after forwarding.

Return values
callable

Returns a function that accepts the next handler.

Search results