final class RequestContext (View source)

Static helpers classifying the current request from server globals: API route detection and JSON response negotiation.

All detection keys off the real routed path, never requestData['page'], so a spoofed ?page=api/... query cannot flip the result.

Constants

HEALTH_PROBE_PATHS

Paths of the unauthenticated monitoring probes, as declared in routes.yaml.

Shared so the session skip below and HeadlessRouteFilter cannot drift apart from each other or from the routes themselves.

Methods

static bool
isApiRequest(bool $requireTrailingSlash = false)

Checks if the current request is any API route

static bool
isInternalApiRoute()

Checks if the current request is an internal API route (api/internal/*)

static bool
isV2ApiRequest()

Whether the current request targets the v2 public API. Matches on the URL path only (query string ignored) so the v2 error-wrapper decision is not swayed by an unrelated URL that merely mentions /api/v2/ in its query string.

static bool
isApiPath()

Whether the request targets an API family declared in routes.yaml. Wider than isApiRequest(), which covers only internal and versioned routes, but matched per segment: a plain /api/ test also caught the web page /settings/api/logs.

static bool
isHealthProbeRequest(string $baseUrlPrefix = '')

Whether the request targets one of the monitoring probes declared in routes.yaml, which are served without a session so a monitor scraping every few seconds does not leave a session file behind per request.

static bool
acceptsJson()

Whether the Accept header mentions JSON at all.

static bool
acceptsJsonOnly()

Whether the Accept header asks for JSON without also accepting HTML.

static bool
isAjax()

Whether the request was made via XMLHttpRequest.

static bool
hasJsonContentType()

Whether the request body is declared as JSON.

static bool
expectsJson()

Checks if the current request expects a JSON response This is more comprehensive than just checking the route

static bool
expectsJsonOnError()

Negotiation used when shaping an unhandled throwable. Deliberately looser than expectsJson(): no Content-Type signal, so a JSON body posted to a web form still gets the HTML error page it can render, and no text/html exclusion on the Accept check, so an API-ish client that also accepts HTML never receives an HTML stack page.

Details

static bool isApiRequest(bool $requireTrailingSlash = false)

Checks if the current request is any API route

Parameters

bool $requireTrailingSlash

Require a path segment after the API root (e.g. /api/v2/zones but not /api/v2); used where a bare root should fall through to web handling

Return Value

bool

True if this is an API request, false otherwise

static bool isInternalApiRoute()

Checks if the current request is an internal API route (api/internal/*)

Return Value

bool

True if this is an internal API route, false otherwise

static bool isV2ApiRequest()

Whether the current request targets the v2 public API. Matches on the URL path only (query string ignored) so the v2 error-wrapper decision is not swayed by an unrelated URL that merely mentions /api/v2/ in its query string.

Return Value

bool

static bool isApiPath()

Whether the request targets an API family declared in routes.yaml. Wider than isApiRequest(), which covers only internal and versioned routes, but matched per segment: a plain /api/ test also caught the web page /settings/api/logs.

Return Value

bool

static bool isHealthProbeRequest(string $baseUrlPrefix = '')

Whether the request targets one of the monitoring probes declared in routes.yaml, which are served without a session so a monitor scraping every few seconds does not leave a session file behind per request.

Matched exactly rather than by pattern: an unanchored regex would also catch paths such as /zones/ping and silently deny them a session.

Parameters

string $baseUrlPrefix

Return Value

bool

static bool acceptsJson()

Whether the Accept header mentions JSON at all.

Return Value

bool

static bool acceptsJsonOnly()

Whether the Accept header asks for JSON without also accepting HTML.

Return Value

bool

static bool isAjax()

Whether the request was made via XMLHttpRequest.

Return Value

bool

static bool hasJsonContentType()

Whether the request body is declared as JSON.

Return Value

bool

static bool expectsJson()

Checks if the current request expects a JSON response This is more comprehensive than just checking the route

Return Value

bool

True if this request expects JSON, false otherwise

static bool expectsJsonOnError()

Negotiation used when shaping an unhandled throwable. Deliberately looser than expectsJson(): no Content-Type signal, so a JSON body posted to a web form still gets the HTML error page it can render, and no text/html exclusion on the Accept check, so an API-ish client that also accepts HTML never receives an HTML stack page.

Return Value

bool

True if a fatal error should be shaped as JSON