class HTTPResponse (View source)

Represents a response returned by a controller.

Traits

A class that can be instantiated or replaced via DI

Properties

protected static array $status_codes
protected static array $redirect_codes
protected string $protocolVersion
protected int $statusCode
protected string $statusDescription
protected array $headers

HTTP Headers like "content-type: text/xml"

protected string $body

Methods

public static 
create(mixed ...$args)

An implementation of the factory method, allows you to create an instance of a class

public static 
singleton(string $class = null)

Creates a class instance by the "singleton" design pattern.

public
__construct(string $body = null, int $statusCode = null, string $statusDescription = null, string $protocolVersion = null)

Create a new HTTP response

public
$this
setProtocolVersion(string $protocolVersion)

The HTTP version used to respond to this request (typically 1.0 or 1.1)

public
$this
setStatusCode(int $code, string $description = null)

No description

public
$this
setStatusDescription(string $description)

The text to be given alongside the status code ("reason phrase").

public
string
getProtocolVersion()

No description

public
int
getStatusCode()

No description

public
string
getStatusDescription()

No description

public
bool
isError()

Returns true if this HTTP response is in error

public
$this
setBody(string $body)

No description

public
string
getBody()

No description

public
$this
addHeader(string $header, string $value)

Add a HTTP header to the response, replacing any header of the same name.

public
string
getHeader(string $header)

Return the HTTP header of the given name.

public
array
getHeaders()

No description

public
$this
removeHeader(string $header)

Remove an existing HTTP header by its name, e.g. "Content-Type".

public
$this
redirect(string $dest, int $code = 302)

No description

public
output()

Send this HTTPResponse to the browser

protected
htmlRedirect()

Generate a browser redirect without setting headers

protected
outputHeaders()

Output HTTP headers to the browser

protected
outputBody()

Output body of this response to the browser

public
bool
isFinished()

Returns true if this response is "finished", that is, no more script execution should be done.

public
bool
isRedirect()

Determine if this response is a redirect

public
string
__toString()

The HTTP response represented as a raw string

Details

static Injectable create(mixed ...$args)

An implementation of the factory method, allows you to create an instance of a class

This method will defer class substitution to the Injector API, which can be customised via the Config API to declare substitution classes.

This can be called in one of two ways - either calling via the class directly, or calling on Object and passing the class name as the first parameter. The following are equivalent: $list = DataList::create(SiteTree::class); $list = SiteTree::get();

Parameters

mixed ...$args

Return Value

Injectable

static Injectable singleton(string $class = null)

Creates a class instance by the "singleton" design pattern.

It will always return the same instance for this class, which can be used for performance reasons and as a simple way to access instance methods which don't rely on instance data (e.g. the custom SilverStripe static handling).

Parameters

string $class

Optional classname to create, if the called class should not be used

Return Value

Injectable

The singleton instance

__construct(string $body = null, int $statusCode = null, string $statusDescription = null, string $protocolVersion = null)

Create a new HTTP response

Parameters

string $body

The body of the response

int $statusCode

The numeric status code - 200, 404, etc

string $statusDescription

The text to be given alongside the status code. See setStatusCode() for more information.

string $protocolVersion

$this setProtocolVersion(string $protocolVersion)

The HTTP version used to respond to this request (typically 1.0 or 1.1)

Parameters

string $protocolVersion

Return Value

$this

$this setStatusCode(int $code, string $description = null)

No description

Parameters

int $code
string $description

Optional. See setStatusDescription(). No newlines are allowed in the description. If omitted, will default to the standard HTTP description for the given $code value (see $status_codes).

Return Value

$this

$this setStatusDescription(string $description)

The text to be given alongside the status code ("reason phrase").

Caution: Will be overwritten by setStatusCode().

Parameters

string $description

Return Value

$this

string getProtocolVersion()

No description

Return Value

string

int getStatusCode()

No description

Return Value

int

string getStatusDescription()

No description

Return Value

string

Description for a HTTP status code

bool isError()

Returns true if this HTTP response is in error

Return Value

bool

$this setBody(string $body)

No description

Parameters

string $body

Return Value

$this

string getBody()

No description

Return Value

string

$this addHeader(string $header, string $value)

Add a HTTP header to the response, replacing any header of the same name.

Parameters

string $header

Example: "content-type"

string $value

Example: "text/xml"

Return Value

$this

string getHeader(string $header)

Return the HTTP header of the given name.

Parameters

string $header

Return Value

string

array getHeaders()

No description

Return Value

array

$this removeHeader(string $header)

Remove an existing HTTP header by its name, e.g. "Content-Type".

Parameters

string $header

Return Value

$this

$this redirect(string $dest, int $code = 302)

No description

Parameters

string $dest
int $code

Return Value

$this

output()

Send this HTTPResponse to the browser

protected htmlRedirect()

Generate a browser redirect without setting headers

protected outputHeaders()

Output HTTP headers to the browser

protected outputBody()

Output body of this response to the browser

bool isFinished()

Returns true if this response is "finished", that is, no more script execution should be done.

Specifically, returns true if a redirect has already been requested

Return Value

bool

bool isRedirect()

Determine if this response is a redirect

Return Value

bool

string __toString()

The HTTP response represented as a raw string

Return Value

string