Namespace: TrishulApi\Core\Http
Author: Shyam Dubey
Since: v1.0.0
The Response class is responsible for constructing and sending HTTP responses in the Trishul API framework. It supports setting status codes, response headers, cookies, sessions, and response bodies. The class is designed to simplify returning JSON responses and managing response-related data in your application.
// Example: Returning a JSON response from a controller
use TrishulApi\Core\Http\Response;
use TrishulApi\Core\Enums\HttpStatus;
$data = [
'success' => true,
'message' => 'Data fetched successfully',
'data' => [/* ... */]
];
// Return a JSON response object (does not terminate execution)
$response = Response::json(HttpStatus::OK, $data);
// Send a JSON response and terminate execution
Response::out(HttpStatus::OK, $data);
// Set a cookie in the response
$cookie = Response::get_cookie();
$cookie->set('token', 'abc123');
Response::set_cookie($cookie);
// Access response headers
$header = Response::get_header();
$header->set('X-Custom-Header', 'value');
private __construct($data, HttpStatus $status, $return_type = Response::RETURN_TYPE_JSON)
Private constructor. Initializes the response object with data, status code, and return type. Use static methods like json() to create a response.
| Parameter | Type | Description |
|---|---|---|
| $data | mixed | Response data to be sent to the client. |
| $status | HttpStatus | HTTP status code (enum). |
| $return_type | string | Type of response (default: JSON). |
public static json(HttpStatus $statusCode, $data): Response
Creates a new Response object with JSON data and the specified status code. Sets the Content-Type header to application/json.
| Parameter | Type | Description |
|---|---|---|
| $statusCode | HttpStatus | HTTP status code. |
| $data | mixed | Data to be returned as JSON. |
Returns: Response object
public static get_body(): ResponseBody
Returns the ResponseBody object containing the response data.
public static get_status_code()
Returns the HTTP status code of the response (e.g., 200, 404, 500).
public static get_return_type()
Returns the return type of the response (currently only JSON is supported).
public static out(HttpStatus $status, $data, $return_type = Response::RETURN_TYPE_JSON): void
Sends the response to the client and terminates execution. Sets the status code and outputs the data as JSON. If an unsupported return type is provided, throws InvalidResponseTypeException.
| Parameter | Type | Description |
|---|---|---|
| $status | HttpStatus | HTTP status code. |
| $data | mixed | Data to be returned. |
| $return_type | string | Response type (default: JSON). |
public static get_header(): Header
Returns the Header object for managing response headers.
public static get_cookie(): Cookie
Returns the Cookie object for managing response cookies.
public static set_cookie(Cookie $cookie): void
Sets the Cookie object for the response.
public static get_session(): Session
Returns the Session object for managing session data in the response.
Response::out() to send a response and terminate execution immediately.out().TrishulApi\Core\Http\HeaderTrishulApi\Core\Http\CookieTrishulApi\Core\Http\SessionTrishulApi\Core\Http\ResponseBodyTrishulApi\Core\Enums\HttpStatusTrishulApi\Core\Exception\InvalidResponseTypeException