Response
Controls HTTP response bodies, content types, status codes, headers, rendering, transmission and orderly runtime termination.
Automatic rendering
If no explicit response body has been assigned, send() obtains the default application
output
from Core\Render. An explicit empty response suppresses this behavior.
Transmission and termination
send() emits the response once per controller lifecycle. exit() sends it and
terminates the runtime through Core\Shutdown.
HTTP status helpers
unauthorized(), forbidden(), notFound() and
methodNotAllowed() terminate when template rendering is enabled.
internalServerError() always terminates with a public HTTP 500 response.
Implementation note
The optional diagnostic argument of internalServerError() does not currently alter the
public
HTTP 500 output.
Example
$response = Registry::get(Response::class);
$response->json(['success' => true])->setStatusCode(200)->send();
Public API Reference
Core\Response
Class & Registry
Class:
Core\Response
Extends:
Core\Response\Base
use Core\Response;
use Core\Registry;
$response = Registry::get(Response::class);
| Method | Description |
|---|---|
init():void |
Initializes the response controller and resolves the shared header controller. |
set(mixed $content):static |
Replaces the response content with the normalized string representation of the supplied value. |
append(mixed $content):static |
Normalizes and appends content to the existing response body. |
html(string $html):static |
Sets the HTML response body and the UTF-8 HTML content type. |
text(string $text):static |
Sets the plain-text response body and the UTF-8 text content type. |
json(object|array|string $data):static |
Sets a JSON response, validating existing JSON strings or encoding objects and arrays with strict error handling. |
xml(string $xml):static |
Sets the XML response body and the UTF-8 XML content type. |
empty(int $statusCode = 204):static |
Creates an empty response with the specified status code and suppresses automatic rendering. |
render(string $template, array $variables = []):static |
Renders a template with the supplied variables and assigns the resulting HTML to the response. |
error(int $statusCode = 500, array $variables = []):static |
Sets an HTTP error status and renders the shared error template with status and title information. |
clear():static |
Resets the response content, HTTP status, content type and internal response state to their defaults. |
getContent():string |
Returns the current response body. |
getStatusCode():int |
Returns the current HTTP response status code. |
setStatusCode(int $statusCode):static |
Sets the HTTP response status code and synchronizes it with the shared header controller. |
getContentType():string|null |
Returns the configured response content type, or null if none is set. |
setContentType(string $contentType):static |
Sets the response content type. |
setHeader(string $header, bool $replace = true):static |
Sets an HTTP response header through the shared header controller, optionally replacing an existing header. |
getSent():bool |
Returns whether the response has already been sent. |
status(int $statusCode, string $message, bool $renderTemplate =
true):static |
Sets an HTTP status and optionally renders the shared status template, falling back to plain text if rendering fails. |
renderHttpStatusCode(string $message):static |
Renders the standalone HTTP status template, using the supplied message as plain text if the rendered body is empty. |
unauthorized(bool $renderTemplate = true):void |
Sets HTTP 401 Unauthorized and terminates the runtime when template rendering is enabled. |
forbidden(bool $renderTemplate = true):void |
Sets HTTP 403 Forbidden and terminates the runtime when template rendering is enabled. |
notFound(bool $renderTemplate = true):void |
Sets HTTP 404 Not Found and terminates the runtime when template rendering is enabled. |
methodNotAllowed(bool $renderTemplate = true):void |
Sets HTTP 405 Method Not Allowed and terminates the runtime when template rendering is enabled. |
internalServerError(array|null $variables = null):never |
Sends a standalone HTTP 500 Internal Server Error response and terminates the runtime. |
send():void |
Sends the response once, automatically rendering application output when no explicit response body has been assigned. |
exit(int $code = 0):never |
Sends the current response and terminates the runtime through the shared shutdown controller. |