Overview
TheResponse class provides methods to send JSON responses with appropriate HTTP status codes.
Methods
response()
Sends a JSON response with a specified HTTP status code.The HTTP response status code (e.g., 200, 404, 500)
The message to include in the JSON response
The key name for the message in the JSON response object
Usage Examples
Success Response
Error Response
Custom Key Response
API Endpoint Example
Common HTTP Status Codes
200- OK (Success)201- Created (Resource created successfully)400- Bad Request (Invalid request data)401- Unauthorized (Authentication required)403- Forbidden (Access denied)404- Not Found (Resource not found)500- Internal Server Error (Server error)
Notes
- This method automatically sets the
Content-Typeheader toapplication/json - The method outputs the JSON response directly and does not return a value
- After calling this method, you typically want to exit the script to prevent further output