This version is outdated. You should upgrade your project to Mako 12.1!
Routing and controllers

Response



The response class helps you build a HTTP response.

An instance of the response class is always available in all controller classes. It is also easily made available in route closures.


Basics

The phpbody method allows you to set the response body. This is not normally needed as the framework automatically sets the response body to the return value of your controller/route action. It can however be useful if you want to alter the response body in an after filter.

public function afterAction()
{
	$this->response->body(strtoupper($this->response->getBody()));
}

The phpgetBody method returns the response body.

$responseBody = $this->response->getBody();

The phptype method lets you define the response type as well as the response charset. The default response type is phptext/html and the default charset is phpUTF-8. The default charset is defined in your phpapp\config\application.php config file.

$this->response->type('application/json');

// You can also override the default charset

$this->response->type('application/json', 'iso-8859-1');

The phpgetType method returns the current response type.

$responseType = $this->response->getType();

The phpcharset method lets you set the response charset. The default charset is phpUTF-8and it is defined in your phpapp\config\application.php config file.

$this->response->charset('iso-8859-1');

The phpgetCharset returns the current response charset.

$responseCharset = $this->response->getCharset();

The phpstatus method lets you set the response status.

$this->response->status(404); // Sends the 404 "Not Found" header.

The phpgetStatus method returns the current status code.

$responseStatus = $this->response->getStatus();

Setting cookies

The phpcookie method adds a cookie to the response.

// Sets a cookie that expires when the browser closes

$this->response->cookie('name', 'value');

// Sets a cookie that expires after 1 hour

$this->response->cookie('name', 'value', 3600);

// You can also set the path, domain, secure and httponly options using the fourth parameter

$this->response->cookie('name', 'value', 3600, ['path' => '/mydir', 'domain' => '.example.org']);

The phpsignedCookie method has the same method signature as the phpcookie method. The difference is that the cookie will be signed using the application secret defined in the phpapp/config/application.php config file.

$this->response->signedCookie('name', 'value');

The benefit of using signed cookies over regular cookies is that their values can not be tampered with on the client site.

The phphasCookie method returns TRUE if the response has the specified cookie and FALSE if not.

$hasCookie = $this->response->hasCookie('name');

The phpremoveCookie method allows you to remove a cookie from the response.

$this->response->removeCookie('name');

The phpdeleteCookie method deletes a cookie from the browser. It can be used to delete both normal and signed cookies.

$this->response->deleteCooke('name');

// You can also set the path, domain, secure and httponly options using the second parameter

$this->response->deleteCookie('name', ['path' => '/mydir', 'domain' => '.example.org']);

The phpgetCookies method returns an array of all the cookies that have been set.

$responseCookies = $this->response->getCookies();

The phpclearCookies method will clear all cookies that have been set.

$this->response->clearCookies();

Setting headers

The phpheader method adds a header to your response. The first parameter is the header field while the second is the header value.

$this->response->header('access-control-allow-origin', '*');

The phphasHeader method returns TRUE if the response has the specified header and FALSE if not.

$hasHeader = $this->response->hasHeader('access-control-allow-origin'):

The phpremoveHeader method allows you to remove a previously set header from the response.

$this->response->removeHeader('access-control-allow-origin');

The phpgetHeaders method will return an array of all the response headers that have been set.

$responseHeaders = $this->response->getHeaders();

The phpclearHeaders method will clear all response headers that have been set

$this->response->clearHeaders();

Response filters

The phpfilter methods allows you to add a response filter that the response body will be passed through before being sent to the browser. You can define multiple filters and they will be executed in the order that they were defined.

$this->response->filter(function($responseBody)
{
	return strtoupper($responseBody);
});

The phpgetFilters method will return an array containing all the defined response filters.

$responseFilters = $this->response->getFilters();

The phpclearFilters method will clear all the defined response filters.

$this->response->clearFilters();

Caching and compression

You can enable phpETag caching using the phpcache method. Doing so can save bandwidth as the response body will only be sent if it has been modified since the last request.

$this->response->cache();

The phpdisableCaching method disables phpETag caching if it has been enabled.

$this->response->disableCaching();

The phpcompress method enables output compression. This will save you bandwidth in exchange for a slight bump in CPU usage.

$this->response->compress();

The phpdisableCompression method disables output compression if it has been enabled.

$this->response->disableCompression();