Response
in package
Class for generating standardized HTTP JSON responses.
Utility class for generating standardized HTTP JSON responses. Provides static methods for common response types including success, error, not found, and validation responses. All responses include a timestamp and automatically set appropriate HTTP status codes.
Table of Contents
Methods
- error() : void
- Sends an error JSON response
- notFound() : void
- Sends a 'not found' JSON response
- success() : void
- Sends a success JSON response
- validation() : void
- Sends a validation error JSON response
Methods
error()
Sends an error JSON response
public
static error([string $message = 'Operation error' ][, int $code = 400 ][, mixed $details = null ]) : void
Outputs a JSON response with error status, custom message, and optional error details. Sets the HTTP status code and terminates execution.
Parameters
- $message : string = 'Operation error'
-
Error message to display
- $code : int = 400
-
HTTP status code (default: 400)
- $details : mixed = null
-
Optional additional error details
Return values
void —Outputs JSON and exits
notFound()
Sends a 'not found' JSON response
public
static notFound([string $message = 'Resource not found' ]) : void
Convenience method that sends a 404 error response.
Parameters
- $message : string = 'Resource not found'
-
Not found message to display
Return values
void —Outputs JSON and exits
success()
Sends a success JSON response
public
static success([mixed $data = null ][, string $message = 'Successful operation' ][, int $code = 200 ]) : void
Outputs a JSON response with success status, optional data payload, and a custom message. Sets the HTTP status code and terminates execution.
Parameters
- $data : mixed = null
-
Optional data to include in the response
- $message : string = 'Successful operation'
-
Success message to display
- $code : int = 200
-
HTTP status code (default: 200)
Return values
void —Outputs JSON and exits
validation()
Sends a validation error JSON response
public
static validation(mixed $errors[, string $message = 'Validation errors' ]) : void
Convenience method that sends a 422 (Unprocessable Entity) error response with validation error details, typically used for form validation failures.
Parameters
- $errors : mixed
-
Validation errors to include in the response details
- $message : string = 'Validation errors'
-
Validation error message to display
Return values
void —Outputs JSON and exits