TrishulApi\Core\Http\Response

Namespace: TrishulApi\Core\Http
Author: Shyam Dubey
Since: v1.0.0

Overview

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.

Usage Example


// 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');
        

Constructor

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.

ParameterTypeDescription
$datamixedResponse data to be sent to the client.
$statusHttpStatusHTTP status code (enum).
$return_typestringType of response (default: JSON).

Methods

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.

ParameterTypeDescription
$statusCodeHttpStatusHTTP status code.
$datamixedData 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.

ParameterTypeDescription
$statusHttpStatusHTTP status code.
$datamixedData to be returned.
$return_typestringResponse 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.

Notes

Related Classes

Changelog