-
Notifications
You must be signed in to change notification settings - Fork 0
JsonResponse
Class JsonResponse.
Provides a JSON-specific HTTP response implementation that complies with PSR-7 Response interfaces. This class MUST be used when returning JSON payloads over HTTP responses. It automatically sets the 'Content-Type' header to 'application/json' with the specified charset.
- Full name:
\FastForward\Http\Message\JsonResponse - Parent class:
Response - This class is marked as final and can't be subclassed
- This class implements:
\FastForward\Http\Message\PayloadResponseInterface - This class is a Final class
Constructs a new JsonResponse instance with an optional payload and charset.
public __construct(mixed $payload = [], string $charset = 'utf-8', array $headers = []): mixedThis constructor SHALL initialize the response body with a JsonStream containing the provided payload. The 'Content-Type' header MUST be set to 'application/json' with the specified charset.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$payload |
mixed | The JSON-serializable payload to send in the response body. Defaults to an empty array. |
$charset |
string | The character encoding to use in the 'Content-Type' header. Defaults to 'utf-8'. |
$headers |
array |
Retrieves the payload contained in the response body.
public getPayload(): mixedThis method MUST return the same payload provided during construction or via withPayload().
Return Value:
the decoded JSON payload
Returns an instance with the specified payload.
public withPayload(mixed $payload): selfThis method SHALL return a new instance of the response with the body replaced by a new JsonStream containing the provided payload. The original instance MUST remain unchanged, ensuring immutability as required by PSR-7.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$payload |
mixed | the new JSON-serializable payload |
Return Value:
a new JsonResponse instance with the updated payload