HttpRequest
Raxos\Http\HttpRequest
An immutable wrapper around an incoming HTTP request, built from the cookies, files, headers, post, query and server maps. It implements Raxos\Contract\Http\HttpRequestInterface.
readonly class HttpRequest implements HttpRequestInterfaceProperties
The request exposes its data as public readonly properties: cookies, files, headers, post, query and server (the structure maps), the parsed method (HttpMethod), the pathName, the full uri and a parameters map.
Methods
public static function create(?HttpCookiesMap $cookies = null, ?HttpFilesMap $files = null, ?HttpHeadersMap $headers = null, ?HttpPostMap $post = null, ?HttpQueryMap $query = null, ?HttpServerMap $server = null, ?HttpMethod $method = null, ?string $uri = null, Map $parameters = new Map()): selfCreates a request for the router, defaulting any missing part from the current global request.
public static function createFromGlobals(): HttpRequestInterfaceBuilds a request entirely from the PHP superglobals.
public function addParameterFromQuery(string $name, string $key, ?callable $sanitizer = null, mixed $defaultValue = null): selfCopies a query value into the parameters map, optionally sanitized, with a fallback default when the key is absent.
public function bearerToken(): ?stringExtracts the bearer token from the Authorization header.
public function contentType(): ?stringReturns the content type without any parameters.
public function ip(): ?IPResolves the client IP from cf-connecting-ip, x-forwarded-for or REMOTE_ADDR into a foundation IP value object.
public function isSecure(): boolReturns true when the request was served over HTTPS.
public function language(): ?stringReturns the first preferred language.
public function languages(): arrayReturns all accepted languages ordered by quality.
public function body(): ?stringReturns the raw request body, or null when it is empty.
public function json(): ?arrayParses the request body as JSON, throwing when the body is present but not valid JSON.
public function userAgent(): ?UserAgentParses the User-Agent header into a UserAgent instance.
Cached accessors
bearerToken(), contentType(), languages(), body(), json() and userAgent() cache their result per request instance, so calling them repeatedly is cheap.
Example
<?php
declare(strict_types=1);
use Raxos\Http\HttpRequest;
$request = HttpRequest::createFromGlobals();
if ($request->isSecure() && $request->contentType() === 'application/json') {
$payload = $request->json();
}
$token = $request->bearerToken();
$language = $request->language();