mako\Response


Description


Mako response class.


Class methods


Toggle source

public __construct($body = NULL)


Constructor.


Parameters

Type Description
string (optional) Response body
Return value

NULL

public function __construct($body = null)
{
	if($body !== null)
	{
		$this->body($body);
	}
}

Toggle source

public static factory($body = NULL)


Factory method making method chaining possible right off the bat.


Parameters

Type Description
string (optional) Response body
Return value

mako\Response

public static function factory($body = null)
{
	return new static($body);
}

Toggle source

public body($body)


Sets the response body.


Parameters

Type Description
string Response body
Return value

NULL

public function body($body)
{
	$this->body = (string) $body;
}

Toggle source

public filter($filter)


Adds output filter that all output will be passed through before being sent.


Parameters

Type Description
callback Callback function used to filter output
Return value

NULL

public function filter($filter)
{
	$this->outputFilter = $filter;
}

Toggle source

public status($statusCode)


Sends HTTP status header.


Parameters

Type Description
int HTTP status code
Return value

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]);
	}
}

Toggle source

public redirect($location = '', $statusCode = 302)


Redirects to another location.


Parameters

Type Description
string (optional) Location
int (optional) HTTP status code
Return value

NULL

public function redirect($location = '', $statusCode = 302)
{
	$this->status($statusCode);

	if(strpos($location, '://') === false)
	{
		$location = URL::to($location);
	}
	
	header('Location: ' . $location);
	
	exit();
}

Toggle source

public cache()


Will enable response cache using ETags.

Return value

NULL

public function cache()
{
	$this->checkEtag = true;
}

Toggle source

public send($statusCode = NULL)


Send output to browser.


Parameters

Type Description
int (optional) HTTP status code
Return value

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;
	}
}

Toggle source

public __toString()


Method that magically converts the response object into a string.

Return value

string

public function __toString()
{
	return $this->body;
}