Response
in package
Class Response
Utility class for handling JSON HTTP responses.
Table of Contents
Methods
- json() : void
- Sends a JSON response.
- jsonError() : void
- Sends a JSON response indicating an error occurred.
- jsonNotFound() : void
- Sends a JSON response indicating a resource was not found.
- jsonSuccess() : void
- Sends a JSON response indicating a successful operation.
- noContent() : void
- Sends a 204 No Content response.
Methods
json()
Sends a JSON response.
public
static json(mixed $data[, int $status = 200 ][, array<string|int, mixed> $headers = [] ][, int $options = 0 ]) : void
Parameters
- $data : mixed
-
The data to be encoded as JSON.
- $status : int = 200
-
The HTTP status code (default is 200).
- $headers : array<string|int, mixed> = []
-
Additional headers to be sent with the response.
- $options : int = 0
-
Options for json_encode (default is 0).
jsonError()
Sends a JSON response indicating an error occurred.
public
static jsonError([string $message = 'An error occurred' ][, int $status = 500 ]) : void
Parameters
- $message : string = 'An error occurred'
-
The error message (default is 'An error occurred').
- $status : int = 500
-
The HTTP status code (default is 500).
jsonNotFound()
Sends a JSON response indicating a resource was not found.
public
static jsonNotFound([string $message = 'Resource not found' ]) : void
Parameters
- $message : string = 'Resource not found'
-
The error message (default is 'Resource not found').
jsonSuccess()
Sends a JSON response indicating a successful operation.
public
static jsonSuccess([mixed $data = [] ], int $status[, string $message = 'Operation successful' ]) : void
Parameters
- $data : mixed = []
-
The data to be included in the response.
- $status : int
-
The HTTP status code.
- $message : string = 'Operation successful'
-
The success message (default is 'Operation successful').
noContent()
Sends a 204 No Content response.
public
static noContent() : void