ApiRequest extends CommonPHP\HTTP\Request with helpers that are useful inside API actions.
ApiSurface automatically adapts requests before dispatch. You can also adapt manually:
use CommonPHP\API\ApiRequest;
use CommonPHP\HTTP\Request;
$apiRequest = ApiRequest::fromRequest($request);If the request is already an ApiRequest, the same instance is returned.
payload() returns an array. It checks sources in this order:
- parsed body array;
- parsed body object converted with
get_object_vars(); - empty array for an empty body;
- decoded JSON body when
Content-Typeis JSON-compatible.
$payload = $request->payload();Non-empty non-JSON bodies throw UnsupportedContentTypeException. Invalid JSON or scalar JSON values throw InvalidRequestException.
Use input() to get the full payload or a single value:
$payload = $request->input();
$email = $request->input('email');
$timezone = $request->input('settings.timezone', 'UTC');hasInput() distinguishes missing keys from keys present with null values:
if ($request->hasInput('deleted_at')) {
// The key was present, even if its value was null.
}When ApiSurface dispatches a matched route, it attaches the RouteMatch and parameters to the request.
$match = $request->routeMatch();
$id = $request->routeParameter('id');
$parameters = $request->routeParameters();Route parameters can also be supplied through request attributes with ApiRequest::ROUTE_PARAMETERS_ATTRIBUTE.