### Install laminas-diactoros via Composer Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/install.md Use Composer to install the laminas-diactoros library. This command adds the package to your project's dependencies. ```bash $ composer require laminas/laminas-diactoros ``` -------------------------------- ### Create and send an HTTP client request with Laminas Diactoros Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/usage.md Shows how to build a Request instance for HTTP client usage, including setting URI, method, headers, and request target. The example also demonstrates writing the request body, sending it via a client, and printing the response. Note that laminas-diactoros does not ship with a client implementation; the send call is illustrative. ```php // Create a request $request = (new Laminas\Diactoros\Request()) ->withUri(new Laminas\Diactoros\Uri('http://example.com')) ->withMethod('PATCH') ->withAddedHeader('Authorization', 'Bearer ' . $token) ->withAddedHeader('Content-Type', 'application/json'); // OR: $request = new Laminas\Diactoros\Request( 'http://example.com', 'PATCH', 'php://memory', [ 'Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json', ] ); // If you want to set a non-origin-form request target, set the // request-target explicitly: $request = $request->withRequestTarget((string) $uri); // absolute-form $request = $request->withRequestTarget($uri->getAuthority()); // authority-form $request = $request->withRequestTarget('*'); // asterisk-form // Once you have the instance: $request->getBody()->write(json_encode($data)); $response = $client->send($request); printf("Response status: %d (%s)\n", $response->getStatusCode(), $response->getReasonPhrase()); printf("Headers:\n"); foreach ($response->getHeaders() as $header => $values) { printf(" %s: %s\n", $header, implode(', ', $values)); } printf("Message:\n%s\n", $response->getBody()); ``` -------------------------------- ### Marshaling an incoming request with ServerRequestFactory::fromGlobals() Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/usage.md Use this to create a ServerRequest instance from PHP superglobals. The method accepts $server, $query, $body, $cookies, and $files in that order; omitted arguments fall back to the corresponding superglobal. When no cookie array is supplied, it parses the cookie header first to preserve original cookie names. ```php $request = Laminas\Diactoros\ServerRequestFactory::fromGlobals( $_SERVER, $_GET, $_POST, $_COOKIE, $_FILES ); ``` -------------------------------- ### Create HtmlResponse and JsonResponse with custom status and headers Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/api.md Shows how to pass custom HTTP status codes and headers, including overriding Content-Type, to HtmlResponse and JsonResponse. The first example uses status 404 with Content-Type application/xhtml+xml; the second uses status 422 with Content-Type application/problem+json. ```php $htmlResponse = new HtmlResponse($html, 404, [ 'Content-Type' => [ 'application/xhtml+xml' ], ]); $jsonResponse = new JsonResponse($data, 422, [ 'Content-Type' => [ 'application/problem+json' ], ]); ``` -------------------------------- ### Manipulating the response: writing body and adding headers Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/usage.md Use the response object to add headers and content. Writing to the body does not change response state, so the return value can be ignored; header manipulation returns a new instance that must be captured. Headers can be added before or after writing body content. ```php $response = new Laminas\Diactoros\Response(); // Write to the response body: $response->getBody()->write("some content\n"); // Multiple calls to write() append: $response->getBody()->write("more content\n"); // now "some content\nmore content\n" // Add headers // Note: headers do not need to be added before data is written to the body! $response = $response ->withHeader('Content-Type', 'text/plain') ->withAddedHeader('X-Show-Something', 'something'); ``` -------------------------------- ### EmptyResponse usage examples Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/custom-responses.md Typical usage of Laminas\Diactoros\Response\EmptyResponse: a basic 204 response, a 201 response with a Location header, and setting the header after instantiation via withHeader(). ```php use Laminas\Diactoros\Response\EmptyResponse; // Basic 204 response: $response = new EmptyResponse(); // 201 response with location header: $response = new EmptyResponse(201, [ 'Location' => [ $url ], ]); // Alternately, set the header after instantiation: $response = (new EmptyResponse(201))->withHeader('Location', $url); ``` -------------------------------- ### RedirectResponse usage examples Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/custom-responses.md Typical usage of Laminas\Diactoros\Response\RedirectResponse: a 302 redirect, a 301 redirect, and using a URI instance (e.g., altering the request URI path). ```php use Laminas\Diactoros\Response\RedirectResponse; // 302 redirect: $response = new RedirectResponse('/user/login'); // 301 redirect: $response = new RedirectResponse('/user/login', 301); // using a URI instance (e.g., by altering the request URI instance) $uri = $request->getUri(); $response = new RedirectResponse($uri->withPath('/login')); ``` -------------------------------- ### Enabling strict Content- header matching with LAMINAS_DIACTOROS_STRICT_CONTENT_HEADER_LOOKUP Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/usage.md Set this environment variable to restrict Content-* header matching to only CONTENT_TYPE, CONTENT_LENGTH, and CONTENT_MD5. This avoids munging unrelated ENV variables into request headers. Available since version 2.6.0; the default behavior remains permissive until version 3.0.0. ```env LAMINAS_DIACTOROS_STRICT_CONTENT_HEADER_LOOKUP=true ``` -------------------------------- ### HTTP header with integer field name Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/security.md Example of an HTTP header field name that is an integer, as allowed by RFC 7230. This can cause issues with PSR-7 implementations because PHP converts integer-like string keys to integers in arrays. ```http 1234: header value ``` -------------------------------- ### Create HtmlResponse and JsonResponse Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/api.md Shows how to instantiate HtmlResponse and JsonResponse with default behavior. HtmlResponse sets Content-Type to text/html, while JsonResponse sets Content-Type to application/json and serializes the data as JSON. ```php $htmlResponse = new HtmlResponse($html); $jsonResponse = new JsonResponse($data); ``` -------------------------------- ### RedirectResponse::__construct Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/custom-responses.md Creates a redirect response to the given URI. The constructor accepts a URI string or UriInterface instance, an optional status code (default 302), and optional headers. ```APIDOC ## RedirectResponse::__construct ### Description Creates a redirect response to the given URI. The constructor accepts a URI string or UriInterface instance, an optional status code (default 302), and optional headers. ### Method Constructor ### Endpoint `Laminas\Diactoros\Response\RedirectResponse::__construct` ### Parameters #### Constructor Parameters - **$uri** (string|Psr\Http\Message\UriInterface) - Required - The URI to redirect to - **$status** (int) - Optional - HTTP status code, defaults to 302 - **$headers** (array) - Optional - Headers for the response ### Response #### Success Response Returns a `RedirectResponse` instance with the given URI and status. ### Request Example ```php use Laminas\Diactoros\Response\RedirectResponse; // 302 redirect: $response = new RedirectResponse('/user/login'); // 301 redirect: $response = new RedirectResponse('/user/login', 301); // using a URI instance (e.g., by altering the request URI instance) $uri = $request->getUri(); $response = new RedirectResponse($uri->withPath('/login')); ``` ``` -------------------------------- ### DoNotFilter::__invoke Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/server-request-filters.md Returns the provided request verbatim without any modifications. ```APIDOC ## DoNotFilter::__invoke ### Description Returns the provided request instance unchanged. ### Method __invoke ### Signature ```php public function __invoke(ServerRequestInterface $request): ServerRequestInterface ``` ### Parameters - **$request** (Psr\Http\Message\ServerRequestInterface) - Required - The request instance to return. ### Return Value Returns the same request instance. ``` -------------------------------- ### Trust any X-Forwarded-* headers Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/server-request-filters.md Creates a filter that trusts all X-Forwarded-* headers from any source. Equivalent to trustProxies(['*']). ```php $filter = FilterUsingXForwardedHeaders::trustAny(); ``` -------------------------------- ### Trust only X-Forwarded-Host from any source Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/server-request-filters.md Creates a filter that trusts only the X-Forwarded-Host header from any source. Uses trustProxies with '0.0.0.0/0' and the HEADER_HOST constant. ```php $filter = FilterUsingXForwardedHeaders::trustProxies('0.0.0.0/0', [FilterUsingXForwardedHeaders::HEADER_HOST]); ``` -------------------------------- ### createUploadedFile Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/api.md Creates an UploadedFile instance from a normal $_FILES entry. ```APIDOC ## createUploadedFile ### Description Creates an UploadedFile instance from a normal $_FILES entry. ### Signature ```php Laminas\Diactoros\createUploadedFile(array $spec) : UploadedFile ``` ### Parameters - **$spec** (array) - Required - A $_FILES entry ### Return Value - **UploadedFile** - UploadedFile instance ``` -------------------------------- ### Laminas\Diactoros\Request\Serializer::toString Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/serialization.md Creates a string from a Request message. This static method serializes a Request instance into a string representation. ```APIDOC ## [STATIC] Laminas\Diactoros\Request\Serializer::toString ### Description Creates a string from a Request message. This static method serializes a Request instance into a string representation. ### Method Static method ### Endpoint Laminas\Diactoros\Request\Serializer::toString(Psr\Http\Message\RequestInterface $message) ### Parameters #### Arguments - **$message** (Psr\Http\Message\RequestInterface) - Required - The message to serialize. ### Response #### Success Response Returns a string representation of the message. #### Exceptions Raises exceptions if required data for serialization is not present in the message instance. ``` -------------------------------- ### Laminas\Diactoros\Request\Serializer::fromString Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/serialization.md Creates a Request instance from a string message. This static method deserializes a string representation of an HTTP request into a Request object. ```APIDOC ## [STATIC] Laminas\Diactoros\Request\Serializer::fromString ### Description Creates a Request instance from a string message. This static method deserializes a string representation of an HTTP request into a Request object. ### Method Static method ### Endpoint Laminas\Diactoros\Request\Serializer::fromString($message) ### Parameters #### Arguments - **$message** (string) - Required - The string message to deserialize. ### Response #### Success Response Returns a `Laminas\Diactoros\Request` instance. #### Exceptions Raises exceptions if errors occur while parsing the message. ``` -------------------------------- ### Create a factory for plain text responses Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/custom-responses.md Use this factory to create a plain text response without extending the Response class. It writes the text to the body and sets the Content-Type header to text/plain only if not already present. The status and headers are the final two arguments, following the recommended semantic. ```php $plainTextResponse = function ($text, $status = 200, array $headers = []) { $response = new Laminas\Diactoros\Response('php://temp', $status, $headers); $response->getBody()->write($text); if (! $response->hasHeader('Content-Type')) { $response = $response->withHeader('Content-Type', 'text/plain'); } return $response; }; $response = $plainTextResponse('Hello, world!'); ``` -------------------------------- ### Laminas\Diactoros\Request\Serializer::fromStream Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/serialization.md Creates a Request instance from a stream. This static method deserializes a stream containing an HTTP request into a Request object. ```APIDOC ## [STATIC] Laminas\Diactoros\Request\Serializer::fromStream ### Description Creates a Request instance from a stream. This static method deserializes a stream containing an HTTP request into a Request object. ### Method Static method ### Endpoint Laminas\Diactoros\Request\Serializer::fromStream(Psr\Http\Message\StreamInterface $stream) ### Parameters #### Arguments - **$stream** (Psr\Http\Message\StreamInterface) - Required - The stream to deserialize. ### Response #### Success Response Returns a `Laminas\Diactoros\Request` instance. #### Exceptions Raises exceptions if errors occur while parsing the message. ``` -------------------------------- ### EmptyResponse::__construct Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/custom-responses.md Creates an empty response with a default 204 status. The constructor accepts an optional status code and headers, and injects an empty read-only body. ```APIDOC ## EmptyResponse::__construct ### Description Creates an empty response with a default 204 status. The constructor accepts an optional status code and headers, and injects an empty read-only body. ### Method Constructor ### Endpoint `Laminas\Diactoros\Response\EmptyResponse::__construct` ### Parameters #### Constructor Parameters - **$status** (int) - Optional - HTTP status code, defaults to 204 - **$headers** (array) - Optional - Headers for the response ### Response #### Success Response Returns an `EmptyResponse` instance with an empty body. ### Request Example ```php use Laminas\Diactoros\Response\EmptyResponse; // Basic 204 response: $response = new EmptyResponse(); // 201 response with location header: $response = new EmptyResponse(201, [ 'Location' => [ $url ], ]); // Alternately, set the header after instantiation: $response = (new EmptyResponse(201))->withHeader('Location', $url); ``` ``` -------------------------------- ### Extend Response class with custom constructor Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/custom-responses.md Use this pattern to create a custom response type by extending Laminas\Diactoros\Response. The constructor can accept custom arguments and must call parent::__construct() because all class properties are private and inaccessible to extensions. This preserves encapsulation and consistency of operations. ```php use Laminas\Diactoros\Response; class MyCustomResponse extends Response { public function __construct($data, $status = 200, array $headers = []) { // - Do something with $data, and create a Stream for the body (if necessary). // - Maybe set some default headers. parent::__construct($body, $status, $headers); } } ``` -------------------------------- ### Laminas\Diactoros\Response\Serializer::fromString Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/serialization.md Creates a Response instance from a string message. This static method deserializes a string representation of an HTTP response into a Response object. ```APIDOC ## [STATIC] Laminas\Diactoros\Response\Serializer::fromString ### Description Creates a Response instance from a string message. This static method deserializes a string representation of an HTTP response into a Response object. ### Method Static method ### Endpoint Laminas\Diactoros\Response\Serializer::fromString($message) ### Parameters #### Arguments - **$message** (string) - Required - The string message to deserialize. ### Response #### Success Response Returns a `Laminas\Diactoros\Response` instance. #### Exceptions Raises exceptions if errors occur while parsing the message. ``` -------------------------------- ### Laminas\Diactoros\Response\Serializer::toString Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/serialization.md Creates a string from a Response message. This static method serializes a Response instance into a string representation. ```APIDOC ## [STATIC] Laminas\Diactoros\Response\Serializer::toString ### Description Creates a string from a Response message. This static method serializes a Response instance into a string representation. ### Method Static method ### Endpoint Laminas\Diactoros\Response\Serializer::toString(Psr\Http\Message\ResponseInterface $message) ### Parameters #### Arguments - **$message** (Psr\Http\Message\ResponseInterface) - Required - The message to serialize. ### Response #### Success Response Returns a string representation of the message. #### Exceptions Raises exceptions if required data for serialization is not present in the message instance. ``` -------------------------------- ### Laminas\Diactoros\Request::__construct Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/api.md Constructor for the Request class, which implements PSR-7 RequestInterface for client-side requests. It initializes the request with URI, method, body, and headers. ```APIDOC ## Request Constructor ### Description Creates a new Request instance for client-side requests. ### Method Constructor ### Endpoint `Laminas\Diactoros\Request::__construct` ### Parameters #### Constructor Parameters - **$uri** (mixed) - Optional - URI for the request, can be a string or Psr\Http\Message\UriInterface. - **$method** (string) - Optional - HTTP method (e.g., GET, POST). - **$body** (mixed) - Optional - Body of the request, can be a string, resource, or Psr\Http\Message\StreamInterface. Defaults to 'php://memory'. - **$headers** (array) - Optional - Array of headers for the request. ### Response No return value (constructor). ### Notes Requests are immutable; methods prefixed with `with` or `without` return a new instance. ``` -------------------------------- ### marshalProtocolVersionFromSapi Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/api.md Extracts the HTTP protocol version from the SAPI server parameters. ```APIDOC ## marshalProtocolVersionFromSapi ### Description Extracts the HTTP protocol version from the SAPI server parameters. ### Signature ```php Laminas\Diactoros\marshalProtocolVersionFromSapi(array $server) : string ``` ### Parameters - **$server** (array) - Required - Server environment array ### Return Value - **string** - Protocol version (e.g., '1.1') ``` -------------------------------- ### Create XML response with default Content-Type Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/custom-responses.md Instantiates an XmlResponse with XML content, setting Content-Type to application/xml by default. ```php $response = new Laminas\Diactoros\Response\XmlResponse($xml); ``` -------------------------------- ### Trust any X-Forwarded-* header from private subnets Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/server-request-filters.md Creates a filter that trusts any X-Forwarded-* header from any private subnet using trustReservedSubnets(). ```php $filter = FilterUsingXForwardedHeaders::trustReservedSubnets(); ``` -------------------------------- ### Create HTML response with default Content-Type Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/custom-responses.md Instantiates an HtmlResponse with HTML content, setting Content-Type to text/html by default. ```php $response = new Laminas\Diactoros\Response\HtmlResponse($htmlContent); ``` -------------------------------- ### ServerRequest class constructor signature Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/api.md Defines the Laminas\Diactoros\ServerRequest class constructor for server-side applications. The constructor accepts server parameters, file parameters, URI, method, body, and headers. The class implements Psr\Http\Message\ServerRequestInterface and is immutable; server parameters are completely immutable and cannot be recalculated. ```php class ServerRequest { public function __construct( array $serverParams = [], array $fileParams = [], $uri = null, $method = null, $body = 'php://input', array $headers = [] ); // See psr/http-message's ServerRequestInterface for other methods. } ``` -------------------------------- ### Uri::__construct Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/api.md Constructor for the Laminas\Diactoros\Uri class. It models and validates URIs, implementing Psr\Http\Message\UriInterface. The constructor accepts an optional URI string. ```APIDOC ## Uri::__construct ### Description Constructor for the Laminas\Diactoros\Uri class. It models and validates URIs, implementing Psr\Http\Message\UriInterface. The constructor accepts an optional URI string. ### Method Constructor ### Endpoint `Laminas\Diactoros\Uri::__construct` ### Parameters #### Constructor Parameters - **$uri** (string) - Optional - The URI string to model. Defaults to an empty string. ### Response No return value (constructor). ``` -------------------------------- ### marshalHeadersFromSapi Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/api.md Extracts HTTP headers from the SAPI server parameters. ```APIDOC ## marshalHeadersFromSapi ### Description Extracts HTTP headers from the SAPI server parameters. ### Signature ```php Laminas\Diactoros\marshalHeadersFromSapi(array $server) : array ``` ### Parameters - **$server** (array) - Required - Server environment array ### Return Value - **array** - Associative array of headers ``` -------------------------------- ### Laminas\Diactoros\ServerRequest::__construct Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/api.md Constructor for the ServerRequest class, which implements PSR-7 ServerRequestInterface for server-side applications. It initializes the request with server parameters, file parameters, URI, method, body, and headers. ```APIDOC ## ServerRequest Constructor ### Description Creates a new ServerRequest instance for server-side applications. ### Method Constructor ### Endpoint `Laminas\Diactoros\ServerRequest::__construct` ### Parameters #### Constructor Parameters - **$serverParams** (array) - Optional - Server parameters (e.g., $_SERVER). - **$fileParams** (array) - Optional - File parameters (e.g., $_FILES). - **$uri** (mixed) - Optional - URI for the request, can be a string or Psr\Http\Message\UriInterface. - **$method** (string) - Optional - HTTP method (e.g., GET, POST). - **$body** (mixed) - Optional - Body of the request, can be a string, resource, or Psr\Http\Message\StreamInterface. Defaults to 'php://input'. - **$headers** (array) - Optional - Array of headers for the request. ### Response No return value (constructor). ### Notes ServerRequest is immutable; methods prefixed with `with` or `without` return a new instance. Server parameters are completely immutable. ``` -------------------------------- ### Trust X-Forwarded-Host and X-Forwarded-Proto from a Class C subnet Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/server-request-filters.md Creates a filter that trusts the X-Forwarded-Host and X-Forwarded-Proto headers from a single Class C subnet (192.168.1.0/24). ```php $filter = FilterUsingXForwardedHeaders::trustProxies( '192.168.1.0/24', [FilterUsingXForwardedHeaders::HEADER_HOST, FilterUsingXForwardedHeaders::HEADER_PROTO] ); ``` -------------------------------- ### URI class constructor and methods Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/api.md Defines the Laminas\Diactoros\Uri class, which implements Psr\Http\Message\UriInterface. The constructor accepts an optional URI string. URIs are immutable; methods prefixed with 'with' or 'without' return a new instance. ```php class Uri { public function __construct($uri = ''); // See psr/http-message's UriInterface for other methods. } ``` -------------------------------- ### new Laminas\Diactoros\Response($body, $statusCode, $headers) Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/api.md Creates a new Response instance implementing Psr\Http\Message\ResponseInterface. The constructor accepts a body stream, status code, and headers. The response is immutable; methods prefixed with 'with' or 'without' return new instances. ```APIDOC ## Constructor: Laminas\Diactoros\Response ### Description Creates a new Response instance implementing Psr\Http\Message\ResponseInterface. The constructor accepts a body stream, status code, and headers. The response is immutable; methods prefixed with 'with' or 'without' return new instances. ### Signature ```php public function __construct( $body = 'php://memory', $statusCode = 200, array $headers = [] ); ``` ### Parameters - **$body** (mixed) - Optional - The body stream resource or string. Defaults to 'php://memory'. - **$statusCode** (int) - Optional - The HTTP status code. Defaults to 200. - **$headers** (array) - Optional - An array of headers. ### Notes - See psr/http-message's ResponseInterface for other methods. - Responses are immutable; methods prefixed with `with` and `without` return new instances. ``` -------------------------------- ### Laminas\Diactoros\Response\Serializer::fromStream Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/serialization.md Creates a Response instance from a stream. This static method deserializes a stream containing an HTTP response into a Response object. ```APIDOC ## [STATIC] Laminas\Diactoros\Response\Serializer::fromStream ### Description Creates a Response instance from a stream. This static method deserializes a stream containing an HTTP response into a Response object. ### Method Static method ### Endpoint Laminas\Diactoros\Response\Serializer::fromStream(Psr\Http\Message\StreamInterface $stream) ### Parameters #### Arguments - **$stream** (Psr\Http\Message\StreamInterface) - Required - The stream to deserialize. ### Response #### Success Response Returns a `Laminas\Diactoros\Response` instance. #### Exceptions Raises exceptions if errors occur while parsing the message. ``` -------------------------------- ### ServerRequestFactory::fromGlobals Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/api.md Creates a new ServerRequest instance from PHP environment superglobals or provided arrays. Accepts optional server, query, body, cookies, files arrays and a request filter. ```APIDOC ## ServerRequestFactory::fromGlobals ### Description Creates a new ServerRequest instance using values from superglobals or provided arrays. ### Method fromGlobals (static) ### Signature ```php Laminas\Diactoros\ServerRequestFactory::fromGlobals( array $server, array $query, array $body, array $cookies, array $files, ?Laminas\Diactoros\ServerRequestFilter\FilterServerRequestInterface $requestFilter ) ``` ### Parameters - **$server** (array) - Optional - Server environment (e.g., $_SERVER) - **$query** (array) - Optional - Query parameters (e.g., $_GET) - **$body** (array) - Optional - Body parameters (e.g., $_POST) - **$cookies** (array) - Optional - Cookie parameters (e.g., $_COOKIE) - **$files** (array) - Optional - Uploaded files (e.g., $_FILES) - **$requestFilter** (?FilterServerRequestInterface) - Optional - Request filter instance; defaults to FilterUsingXForwardedHeaders::trustReservedSubnets() in v2, DoNotFilter in v3 ### Return Value - **ServerRequest** - New ServerRequest instance ### Request Example ```php // Returns new ServerRequest instance, using values from superglobals: $request = ServerRequestFactory::fromGlobals(); // or // Returns new ServerRequest instance, using values provided (in this // case, equivalent to the previous!) $request = ServerRequestFactory::fromGlobals( $_SERVER, $_GET, $_POST, $_COOKIE, $_FILES ); ``` ``` -------------------------------- ### Create ServerRequest with explicit values Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/api.md Pass $_SERVER, $_GET, $_POST, $_COOKIE, and $_FILES to fromGlobals() to create a ServerRequest instance equivalent to using superglobals. ```php // Returns new ServerRequest instance, using values provided (in this // case, equivalent to the previous!) $request = ServerRequestFactory::fromGlobals( $_SERVER, $_GET, $_POST, $_COOKIE, $_FILES ); ``` -------------------------------- ### marshalMethodFromSapi Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/api.md Extracts the HTTP method from the SAPI server parameters. ```APIDOC ## marshalMethodFromSapi ### Description Extracts the HTTP method from the SAPI server parameters. ### Signature ```php Laminas\Diactoros\marshalMethodFromSapi(array $server) : string ``` ### Parameters - **$server** (array) - Required - Server environment array ### Return Value - **string** - HTTP method (e.g., 'GET') ``` -------------------------------- ### FilterServerRequestInterface::__invoke Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/server-request-filters.md Defines the contract for server request filters. Implementations modify the initial state of a ServerRequest instance. ```APIDOC ## FilterServerRequestInterface::__invoke ### Description Invokes the filter to modify the provided ServerRequest instance. ### Method __invoke ### Signature ```php public function __invoke(ServerRequestInterface $request): ServerRequestInterface ``` ### Parameters - **$request** (Psr\Http\Message\ServerRequestInterface) - Required - The request instance to filter. ### Return Value Returns the modified ServerRequestInterface instance. ``` -------------------------------- ### RedirectResponse class signature Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/custom-responses.md Constructor signature for Laminas\Diactoros\Response\RedirectResponse. Requires a URI (string or Psr\Http\Message\UriInterface); defaults to status 302 with no extra headers, both overridable via optional arguments. ```php namespace Laminas\Diactoros\Response; use Laminas\Diactoros\Response; class RedirectResponse extends Response { public function __construct($uri, $status = 302, array $headers = []); } ``` -------------------------------- ### Request class constructor signature Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/api.md Defines the Laminas\Diactoros\Request class constructor for client-side requests. The constructor accepts optional URI, method, body, and headers. The class implements Psr\Http\Message\RequestInterface and is immutable; methods prefixed with 'with' or 'without' return new instances. ```php class Request { public function __construct( $uri = null, $method = null, $body = 'php://memory', array $headers = [] ); // See psr/http-message's RequestInterface for other methods } ``` -------------------------------- ### Create a TextResponse with default content type Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/custom-responses.md Creates a plain text response using Laminas\Diactoros\Response\TextResponse. Sets the Content-Type header to text/plain by default. ```php $response = new Laminas\Diactoros\Response\TextResponse('Hello world!'); ``` -------------------------------- ### Trust X-Forwarded-Host from Class A or Class C subnets Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/server-request-filters.md Creates a filter that trusts the X-Forwarded-Host header from either a Class A (10.1.1.0/16) or a Class C (192.168.1.0/24) subnet. ```php $filter = FilterUsingXForwardedHeaders::trustProxies( ['10.1.1.0/16', '192.168.1.0/24'], [FilterUsingXForwardedHeaders::HEADER_HOST, FilterUsingXForwardedHeaders::HEADER_PROTO] ); ``` -------------------------------- ### UploadedFile Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/api.md The Laminas\Diactoros\UploadedFile class implements Psr\Http\Message\UploadedFileInterface, providing abstraction around a single uploaded file, including behavior for interacting with it as a stream or moving it to a filesystem location. ```APIDOC ## UploadedFile ### Description The Laminas\Diactoros\UploadedFile class implements Psr\Http\Message\UploadedFileInterface, providing abstraction around a single uploaded file, including behavior for interacting with it as a stream or moving it to a filesystem location. ### Methods This class implements the methods defined in `Psr\Http\Message\UploadedFileInterface`. Refer to that interface for the full list of methods. ### Usage In most cases, you will only use the methods defined in the `UploadedFileInterface`. ``` -------------------------------- ### Response class constructor signature Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/api.md Shows the constructor signature for Laminas\Diactoros\Response, which implements Psr\Http\Message\ResponseInterface. The body defaults to 'php://memory', status code to 200, and headers to an empty array. Responses are immutable; methods prefixed with 'with' or 'without' return new instances. ```php class Response { public function __construct( $body = 'php://memory', $statusCode = 200, array $headers = [] ); // See psr/http-message's ResponseInterface for other methods } ``` -------------------------------- ### Array serialization in a logger middleware Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/serialization.md Uses ArraySerializer::toArray() to convert request and response objects into arrays for logging. The middleware logs the arrays with the debug level, then returns the response. ```php class LoggerMiddleware { /** * @var \Psr\Log\LoggerInterface */ protected $logger; public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { $response = $next($request, $response); $this->logger->debug('Request/Response', [ 'request' => \Laminas\Diactoros\Request\ArraySerializer::toArray($request), 'response' => \Laminas\Diactoros\Response\ArraySerializer::toArray($response), ]); return $response; } } ``` -------------------------------- ### Stream::__construct Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/api.md Constructor for the Laminas\Diactoros\Stream class. It provides facilities around manipulating a PHP stream resource. The constructor accepts a stream identifier or a PHP stream resource, and optionally a file mode. ```APIDOC ## Stream::__construct ### Description Constructor for the Laminas\Diactoros\Stream class. It provides facilities around manipulating a PHP stream resource. The constructor accepts a stream identifier or a PHP stream resource, and optionally a file mode. ### Method Constructor ### Endpoint `Laminas\Diactoros\Stream::__construct` ### Parameters #### Constructor Parameters - **$stream** (string|resource) - Required - A stream identifier (e.g., `php://input`, a filename) or a PHP stream resource. - **$mode** (string) - Optional - The file mode to use when opening the stream, if a stream identifier is provided. Defaults to `'wb+'` for Response objects and `'rb'` for ServerRequest objects. ### Response No return value (constructor). ``` -------------------------------- ### normalizeServer Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/api.md Normalizes the server array, aggregating the Authorization header when under Apache. Accepts an optional callback for Apache request header handling. ```APIDOC ## normalizeServer ### Description Normalizes the server array, aggregating the Authorization header in SAPI params when under Apache. ### Signature ```php Laminas\Diactoros\normalizeServer(array $server, callable $apacheRequestHeaderCallback = null) : array ``` ### Parameters - **$server** (array) - Required - Server environment array - **$apacheRequestHeaderCallback** (callable) - Optional - Callback for Apache request headers ### Return Value - **array** - Normalized server array ``` -------------------------------- ### new Laminas\Diactoros\Response\HtmlResponse($html, $statusCode, $headers) Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/api.md Creates an HTML response with a stream containing the provided HTML string. The Content-Type header is set to 'text/html' by default, but can be overridden via the headers parameter. ```APIDOC ## Constructor: Laminas\Diactoros\Response\HtmlResponse ### Description Creates an HTML response with a stream containing the provided HTML string. The Content-Type header is set to 'text/html' by default, but can be overridden via the headers parameter. ### Signature ```php new HtmlResponse($html); new HtmlResponse($html, 404, [ 'Content-Type' => [ 'application/xhtml+xml' ], ]); ``` ### Parameters - **$html** (string) - Required - The HTML content for the response body. - **$statusCode** (int) - Optional - The HTTP status code. Defaults to 200. - **$headers** (array) - Optional - Additional headers, including Content-Type. ### Response - The stream contains the HTML string. - The Content-Type header is set to `text/html` by default, or to the provided value. ### Request Example ```php $htmlResponse = new HtmlResponse($html); ``` ### Request Example with Status and Headers ```php $htmlResponse = new HtmlResponse($html, 404, [ 'Content-Type' => [ 'application/xhtml+xml' ], ]); ``` ``` -------------------------------- ### Create ServerRequest from superglobals Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/api.md Use ServerRequestFactory::fromGlobals() without arguments to create a ServerRequest instance from PHP superglobals. ```php // Returns new ServerRequest instance, using values from superglobals: $request = ServerRequestFactory::fromGlobals(); ``` -------------------------------- ### Create HTML response with custom status and headers Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/custom-responses.md Passes a status code and headers array to override the default Content-Type header. ```php $response = new Laminas\Diactoros\Response\HtmlResponse( $htmlContent, 200, ['Content-Type' => ['application/xhtml+xml']] ); ``` -------------------------------- ### Laminas\Diactoros\Request\ArraySerializer::fromArray Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/serialization.md Creates a Request instance from an array. This static method deserializes an array representation of an HTTP request into a Request object. ```APIDOC ## [STATIC] Laminas\Diactoros\Request\ArraySerializer::fromArray ### Description Creates a Request instance from an array. This static method deserializes an array representation of an HTTP request into a Request object. ### Method Static method ### Endpoint Laminas\Diactoros\Request\ArraySerializer::fromArray(array $message) ### Parameters #### Arguments - **$message** (array) - Required - The array message to deserialize. ### Response #### Success Response Returns a `Laminas\Diactoros\Request` instance. #### Exceptions Raises exceptions if errors occur while parsing the message. ``` -------------------------------- ### new Laminas\Diactoros\Response\JsonResponse($data, $statusCode, $headers) Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/api.md Creates a JSON response with a stream containing the JSON-serialized data. The Content-Type header is set to 'application/json' by default, but can be overridden via the headers parameter. ```APIDOC ## Constructor: Laminas\Diactoros\Response\JsonResponse ### Description Creates a JSON response with a stream containing the JSON-serialized data. The Content-Type header is set to 'application/json' by default, but can be overridden via the headers parameter. ### Signature ```php new JsonResponse($data); new JsonResponse($data, 422, [ 'Content-Type' => [ 'application/problem+json' ], ]); ``` ### Parameters - **$data** (mixed) - Required - The data to serialize as JSON. - **$statusCode** (int) - Optional - The HTTP status code. Defaults to 200. - **$headers** (array) - Optional - Additional headers, including Content-Type. ### Response - The stream contains the JSON-serialized data. - The Content-Type header is set to `application/json` by default, or to the provided value. ### Request Example ```php $jsonResponse = new JsonResponse($data); ``` ### Request Example with Status and Headers ```php $jsonResponse = new JsonResponse($data, 422, [ 'Content-Type' => [ 'application/problem+json' ], ]); ``` ``` -------------------------------- ### Response class constructor signature Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/custom-responses.md Shows the standard Response constructor signature from Laminas\Diactoros, which takes a body, status code, and headers. This is the default response type that custom response types simplify. ```php namespace Laminas\Diactoros; use Psr\Http\Message\ResponseInterface; class Response implements ResponseInterface { public function __construct($body = 'php://temp', $status = 200, array $headers = []); } ``` -------------------------------- ### FilterUsingXForwardedHeaders::trustAny Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/server-request-filters.md Configures the filter to trust requests from any origin and use any X-Forwarded-* headers. ```APIDOC ## FilterUsingXForwardedHeaders::trustAny ### Description Sets the filter to trust requests from any origin and use any of the X-Forwarded-* headers. ### Method trustAny ### Signature ```php public static function trustAny(): void ``` ### Parameters None. ### Return Value Void. ### Example ```php $filter = FilterUsingXForwardedHeaders::trustAny(); ``` ``` -------------------------------- ### EmptyResponse class signature Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/custom-responses.md Constructor signature for Laminas\Diactoros\Response\EmptyResponse. Accepts an optional status (default 204) and headers array; an empty read-only body is injected at instantiation, preventing write operations. ```php namespace Laminas\Diactoros\Response; use Laminas\Diactoros\Response; class EmptyResponse extends Response { public function __construct($status = 204, array $headers = []); } ``` -------------------------------- ### Create XML response with custom status and headers Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/custom-responses.md Passes a status code and headers array to override the default Content-Type header. ```php $response = new Laminas\Diactoros\Response\XmlResponse( $xml, 200, ['Content-Type' => ['application/hal+xml']] ); ``` -------------------------------- ### TextResponse with custom status and headers Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/custom-responses.md Creates a TextResponse with additional constructor arguments: a status code and an array of headers. Overrides the default Content-Type header with text/csv. ```php $response = new Laminas\Diactoros\Response\TextResponse( $text, 200, ['Content-Type' => ['text/csv']] ); ``` -------------------------------- ### JsonResponse Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/custom-responses.md Creates a JSON response with a default Content-Type of application/json. The constructor accepts optional status code, headers, and JSON encoding flags. ```APIDOC ## JsonResponse ### Description Creates a JSON response with a default Content-Type of application/json. The constructor accepts optional status code, headers, and JSON encoding flags. ### Constructor `new Laminas\Diactoros\Response\JsonResponse($data, $status = 200, array $headers = [], $encodingOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT)` ### Parameters - `$data` (mixed) - Required - Data to convert to JSON. If an object, it should implement JsonSerializable. - `$status` (int) - Optional - HTTP status code (default: 200). - `$headers` (array) - Optional - Headers to set, overriding the default Content-Type if provided. - `$encodingOptions` (int) - Optional - Flags for `json_encode()`. Defaults to `JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT` (integer 15). ### Response Sets `Content-Type` header to `application/json` by default. ### Request Example ```php $response = new Laminas\Diactoros\Response\JsonResponse($data); ``` ### Request Example with custom status and headers ```php $response = new Laminas\Diactoros\Response\JsonResponse( $data, 200, ['Content-Type' => ['application/hal+json']] ); ``` ### Request Example with custom encoding flags ```php $response = new Laminas\Diactoros\Response\JsonResponse( $data, 200, [], JSON_PRETTY_PRINT | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT ); ``` ``` -------------------------------- ### HtmlResponse Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/custom-responses.md Creates an HTML response with a default Content-Type of text/html. The constructor accepts optional status code and headers to customize the response. ```APIDOC ## HtmlResponse ### Description Creates an HTML response with a default Content-Type of text/html. The constructor accepts optional status code and headers to customize the response. ### Constructor `new Laminas\Diactoros\Response\HtmlResponse($htmlContent, $status = 200, array $headers = [])` ### Parameters - `$htmlContent` (string) - Required - The HTML content for the response body. - `$status` (int) - Optional - HTTP status code (default: 200). - `$headers` (array) - Optional - Headers to set, overriding the default Content-Type if provided. ### Response Sets `Content-Type` header to `text/html` by default. ### Request Example ```php $response = new Laminas\Diactoros\Response\HtmlResponse($htmlContent); ``` ### Request Example with custom status and headers ```php $response = new Laminas\Diactoros\Response\HtmlResponse( $htmlContent, 200, ['Content-Type' => ['application/xhtml+xml']] ); ``` ``` -------------------------------- ### FilterUsingXForwardedHeaders::trustReservedSubnets Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/server-request-filters.md Configures the filter to trust requests from reserved private subnets. ```APIDOC ## FilterUsingXForwardedHeaders::trustReservedSubnets ### Description Sets the filter to trust requests from reserved, private subnets. ### Method trustReservedSubnets ### Signature ```php public static function trustReservedSubnets(): void ``` ### Parameters None. ### Return Value Void. ### Example ```php $filter = FilterUsingXForwardedHeaders::trustReservedSubnets(); ``` ``` -------------------------------- ### Create JSON response with custom status and headers Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/custom-responses.md Passes a status code and headers array to override the default Content-Type header. ```php $response = new Laminas\Diactoros\Response\JsonResponse( $data, 200, ['Content-Type' => ['application/hal+json']] ); ``` -------------------------------- ### Create JSON response with default Content-Type Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/custom-responses.md Instantiates a JsonResponse with a data structure, setting Content-Type to application/json. For objects, implement JsonSerializable to ensure correct serialization. ```php $response = new Laminas\Diactoros\Response\JsonResponse($data); ``` -------------------------------- ### FilterServerRequestInterface definition Source: https://github.com/laminas/laminas-diactoros/blob/3.10.x/docs/book/v3/server-request-filters.md Defines the contract for server request filters. Implementations must be invokable and return a modified ServerRequestInterface instance. ```php namespace Laminas\Diactoros\ServerRequestFilter; use Psr\Http\Message\ServerRequestInterface; interface FilterServerRequestInterface { public function __invoke(ServerRequestInterface $request): ServerRequestInterface; } ```