RequestContext
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
Checks if the current request is any API route
Checks if the current request is an internal API route (api/internal/*)
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.
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.
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.
Whether the Accept header mentions JSON at all.
Whether the Accept header asks for JSON without also accepting HTML.
Whether the request was made via XMLHttpRequest.
Whether the request body is declared as JSON.
Checks if the current request expects a JSON response This is more comprehensive than just checking the route
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
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.
Matched exactly rather than by pattern: an unanchored regex would also catch paths such as /zones/ping and silently deny them a session.
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.