Pickles Framework Documentation

Response
in package

Class Response

Represents an HTTP response to be sent to the client. Allows setting status code, headers, and body content. Includes helper methods for common response types (JSON, text, redirect).

Table of Contents

Properties

$content  : string|null
The body content of the response.
$headers  : array<string, string>
HTTP headers to be sent with the response.
$status  : int
The HTTP status code of the response (default: 200 OK).

Methods

getContent()  : string|null
Get the response content.
getHeaders()  : array<string, string>|string|null
Get all headers set in the response.
getStatus()  : int
Get the current HTTP status code.
json()  : self
Create a JSON response.
prepare()  : void
Prepare the response by adjusting headers based on content presence.
redirect()  : self
Create a redirect response to the given URI.
removeHeader()  : void
Remove a specific header.
setContent()  : $this
Set the response content.
setContentType()  : $this
Set the Content-Type header.
setHeader()  : $this
Set a specific header with a value.
setHeaders()  : $this
Replace all headers with a new set.
setStatus()  : $this
Set the HTTP status code.
text()  : self
Create a plain text response.
view()  : self
Creates a new instance of the current class with rendered HTML content from a view.
withErrors()  : self
Attach error messages and old input data to the session and set the HTTP status code.

Properties

$content

The body content of the response.

protected string|null $content = null

$headers

HTTP headers to be sent with the response.

protected array<string, string> $headers = []

Keys are header names (lowercase), and values are header values.

$status

The HTTP status code of the response (default: 200 OK).

protected int $status = 200

Methods

getContent()

Get the response content.

public getContent() : string|null
Return values
string|null

getHeaders()

Get all headers set in the response.

public getHeaders([string $key = null ]) : array<string, string>|string|null
Parameters
$key : string = null

Headers key

Return values
array<string, string>|string|null

getStatus()

Get the current HTTP status code.

public getStatus() : int
Return values
int

json()

Create a JSON response.

public static json(array<string|int, mixed> $data) : self
Parameters
$data : array<string|int, mixed>

The data to encode as JSON.

Return values
self

prepare()

Prepare the response by adjusting headers based on content presence.

public prepare() : void

If content is null, removes Content-Type and Content-Length. Otherwise, sets Content-Length based on content length.

redirect()

Create a redirect response to the given URI.

public static redirect(string $uri) : self
Parameters
$uri : string

The target URI to redirect to.

Return values
self

removeHeader()

Remove a specific header.

public removeHeader(HttpHeader $header) : void
Parameters
$header : HttpHeader

The header to remove.

setContent()

Set the response content.

public setContent(string|null $content) : $this
Parameters
$content : string|null
Return values
$this

setContentType()

Set the Content-Type header.

public setContentType(string $content) : $this
Parameters
$content : string

MIME type (e.g., application/json)

Return values
$this

setHeader()

Set a specific header with a value.

public setHeader(HttpHeader|string $header, string $value) : $this
Parameters
$header : HttpHeader|string

The header name (enum or string).

$value : string

The header value.

Return values
$this

setHeaders()

Replace all headers with a new set.

public setHeaders(array<string, string> $headers) : $this
Parameters
$headers : array<string, string>
Return values
$this

setStatus()

Set the HTTP status code.

public setStatus(int $status) : $this
Parameters
$status : int
Return values
$this

text()

Create a plain text response.

public static text(string $text) : self
Parameters
$text : string

The text content.

Return values
self

view()

Creates a new instance of the current class with rendered HTML content from a view.

public static view(string $view[, array<string|int, mixed> $params = [] ][, string|null $layout = null ]) : self

This static factory method resolves the Kernel from the dependency container, uses its view engine to render the specified view, and returns a new instance with the rendered content and appropriate content type.

Parameters
$view : string

The name of the view to render (without .php extension).

$params : array<string|int, mixed> = []

An associative array of parameters to be extracted into the view scope.

$layout : string|null = null

Optional layout name. If null, the engine's default layout is used.

Tags
throws
RuntimeException

If the resolved Kernel instance is not of the expected type.

Return values
self

A new instance of the class with HTML content set.

withErrors()

Attach error messages and old input data to the session and set the HTTP status code.

public withErrors(array<string|int, mixed> $errors[, int $status = 400 ]) : self

This method is typically used to handle validation errors or other error scenarios where you want to provide feedback to the user and preserve their input data.

Parameters
$errors : array<string|int, mixed>

An array of error messages to be flashed to the session.

$status : int = 400

The HTTP status code to set for the response. Defaults to 400.

Return values
self

Returns the current instance for method chaining.


        
On this page

Search results