Mako response class.
Constructor.
| Type | Description |
|---|---|
| string | (optional) Response body |
NULL
public function __construct($body = null)
{
if($body !== null)
{
$this->body($body);
}
}
Factory method making method chaining possible right off the bat.
| Type | Description |
|---|---|
| string | (optional) Response body |
mako\Response
public static function factory($body = null)
{
return new static($body);
}
Sets the response body.
| Type | Description |
|---|---|
| string | Response body |
NULL
public function body($body)
{
$this->body = (string) $body;
}
Adds output filter that all output will be passed through before being sent.
| Type | Description |
|---|---|
| callback | Callback function used to filter output |
NULL
public function filter($filter)
{
$this->outputFilter = $filter;
}
Sends HTTP status header.
| Type | Description |
|---|---|
| int | HTTP status code |
NULL
public function status($statusCode)
{
if(isset($this->statusCodes[$statusCode]))
{
if(isset($_SERVER['FCGI_SERVER_VERSION']))
{
$protocol = 'Status:';
}
else
{
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
}
header($protocol . ' ' . $statusCode . ' '. $this->statusCodes[$statusCode]);
}
}
Redirects to another location.
| Type | Description |
|---|---|
| string | (optional) Location |
| int | (optional) HTTP status code |
NULL
public function redirect($location = '', $statusCode = 302)
{
$this->status($statusCode);
if(strpos($location, '://') === false)
{
$location = URL::to($location);
}
header('Location: ' . $location);
exit();
}
Will enable response cache using ETags.
NULL
public function cache()
{
$this->checkEtag = true;
}
Send output to browser.
| Type | Description |
|---|---|
| int | (optional) HTTP status code |
NULL
public function send($statusCode = null)
{
if($statusCode !== null)
{
$this->status($statusCode);
}
// Print output to browser (if there is any)
if($this->body !== '')
{
// Pass output through filter
if(!empty($this->outputFilter))
{
$this->body = call_user_func($this->outputFilter, $this->body);
}
// Check ETag
if($this->checkEtag === true)
{
$hash = '"' . sha1($this->body) . '"';
header('ETag: ' . $hash);
if(isset($_SERVER['HTTP_IF_NONE_MATCH']) && $hash === $_SERVER['HTTP_IF_NONE_MATCH'])
{
$this->status(304);
return; // Don't send any output
}
}
// Compress output (if enabled)
if(Config::get('mako.compress_output') === true)
{
ob_start('ob_gzhandler');
}
echo $this->body;
}
}
Method that magically converts the response object into a string.
string
public function __toString()
{
return $this->body;
}