Cakephp Tip #3 – Requesting JSON or XML

The newest version of Cakephp makes it even easier to make a JSON or XML request.

Add the request handler component to your controller;

public $components = array('Paginator','RequestHandler' );

create your action in your controller;
eg

public function testjson() {
$this->set('posts', $this->paginate());
$this->set('_serialize', array('posts'));
}

To access either the JSON or XML version simply append .json or .xml to the action name;

http://localhost/myapp/posts/testjson.json

http://localhost/myapp/posts/testjson.xml

Manual: