Response object

The response objects are just a way of collecting all the content that will be sent back to the browser, or whatever made the request.

This is nearly always going to be the HTML response, which builds its content using a view and template.

If you are not responding with some HTML (e.g. an image), then you will probably prefer to handle the response yourself, where the following helper functions may be useful:


However, for completeness, there are file and text response objects (see below).

This is kind of like the Symfony Response.

HTML Response

When your in a controller, you will typically get the current response object with:

$response = response_get();

A reminder of the main methods can be found in the debug [H] notes, but in summary, these are:

To change to a different template:


To use a different view than the default, or in special cases add custom HTML:

$response->view_path_set(VIEW_ROOT . '/file.ctp');

To set the ID for the page (on the <body> tag):


To set the page <title>:

$response->title_set('Custom page title.');
$response->title_full_set('Custom page title.');

To set the page description:

$response->description_set('Page description');

Additional CSP sources:

$response->csp_source_add('script-src', array(''));

To add some JavaScript or CSS (details):


$response->css_alternate_add('/path/to/file.css', 'print');
$response->css_alternate_add('/path/to/file.css', 'all', 'Title');

To add meta tags, which are ideal for providing variables to JavaScript:

$response->meta_set('js_data', json_encode($x));

Or just to add your own HTML to the page head (avoid JavaScript though):


Typically you just then leave the HTML response for the framework to send it for you.

But if you have an error, you can use the global error_send() function, which is a shortcut for:


Flush early

If you are pushing the performance side of page loading, it is possible add the following to your controller:

sleep(1); // Testing

Or for an example which might included a form, and automatically selected CSS:

if (config::get('request.method') == 'GET') {

    // csrf_token_get();

    $response = response_get();


This will start sending your <head> to the browser so it can start downloading some external resources (i.e. css).

Also see


File Response

You will need to request a new response:

$response = response_get('file');

Then provide it with the required information:

$response->charset_set('UTF-8'); // Defaults to output.charset

Where the content can be added with:


// or


And to send the response:


Text Response

Pretty much the same as above:

$response = response_get('text');
$response->charset_set('UTF-8'); // Defaults to output.charset
$response->inline_set(false); // Probably not needed
$response->name_set('data.csv'); // Probably not needed



JSON Response

To make JSON responses easier:

$response = response_get('json');
$response->send(['name' => 'value']);

Where you can use pretty printing: