returning JSON from a view in CakePHP 1.1

I know CakePHP 3.5 is out, and before CakePHP 2.x and even before CakePHP 1.3 allowed to render JSON easily. But suppose you have a large code-base in CakePHP 1.1 you have to maintain, and suddenly someone wants JSON out of it…

If upgrading your CakePHP version is possible, do it. Else, consider the code below.

In your controller, find() your records :

function myctrfunc() {
    $people = array('name' => 'Doe', 'given' => 'John');
    Configure::write('debug', 0); // to avoid polluting the JSON
    $this->set('people', $people);
    $this->layout = "json";
    $this->autoLayout = false;

In your views/yurctrl/myctrfunc.thtml view, encode to JSON:

header('Content-Type: application/json'); 
echo json_encode($people); 

create the layout you selected in the controler in app/views/layouts/json.thtml:

echo $content_for_layout; 

Success, you will get JSON in the browser:

"name": "Doe",
"given": "John"