JsonResponse
in package
implements
ResponseInterface
HTTP JSON response.
Encapsulates a JSON response with data and status code. Can be returned from controllers and sent by the router.
Tags
Table of Contents
Interfaces
- ResponseInterface
- Interface for HTTP response objects.
Properties
- $data : mixed
- $encodingFlags : int
- $statusCode : int
Methods
- __construct() : mixed
- Create a new JSON response.
- created() : self
- Create a created response (201 Created).
- error() : self
- Create an error response.
- getData() : mixed
- Get the response data.
- getStatusCode() : int
- Get the HTTP status code.
- notFound() : self
- Create a not found response (404 Not Found).
- send() : void
- Send the JSON response.
- success() : self
- Create a success response (200 OK).
Properties
$data
private
mixed
$data
Response data to encode as JSON
$encodingFlags
private
int
$encodingFlags
JSON encoding flags
$statusCode
private
int
$statusCode
HTTP status code
Methods
__construct()
Create a new JSON response.
public
__construct(mixed $data[, int $statusCode = 200 ][, int $encodingFlags = 0 ]) : mixed
Parameters
- $data : mixed
-
Data to encode as JSON
- $statusCode : int = 200
-
HTTP status code (default: 200)
- $encodingFlags : int = 0
-
JSON encoding flags (default: 0)
created()
Create a created response (201 Created).
public
static created(mixed $data) : self
Parameters
- $data : mixed
-
Response data
Return values
selferror()
Create an error response.
public
static error(string $message[, int $statusCode = 400 ]) : self
Parameters
- $message : string
-
Error message
- $statusCode : int = 400
-
HTTP status code (default: 400)
Return values
selfgetData()
Get the response data.
public
getData() : mixed
Return values
mixed —Response data
getStatusCode()
Get the HTTP status code.
public
getStatusCode() : int
Return values
int —HTTP status code
notFound()
Create a not found response (404 Not Found).
public
static notFound([string $message = 'Not found' ]) : self
Parameters
- $message : string = 'Not found'
-
Error message (default: "Not found")
Return values
selfsend()
Send the JSON response.
public
send() : void
success()
Create a success response (200 OK).
public
static success(mixed $data) : self
Parameters
- $data : mixed
-
Response data