xpepermint
xpepermint

Reputation: 36243

CakePHP JSON extension?

I've created a simple REST service that serves data as XML. I've managed to enable XML, JS and RSS format but I can not find the way to enable JSON format. Is JS == JSON? Guess not :).

How can I enable this in version 1.2/1.3?

Thx!!

Upvotes: 0

Views: 4475

Answers (4)

p27
p27

Reputation: 2222

just add this line of code in your controller or AppController

var $components = array('RequestHandler');

function beforeFilter() {
    $this->RequestHandler->setContent('json', 'text/x-json');
}

and run it into internet explorer.

Upvotes: 0

neilcrookes
neilcrookes

Reputation: 4203

Router::parseExtensions('json');

Upvotes: 3

Travis Leleu
Travis Leleu

Reputation: 4230

Quick google search indicates that there are is a json Component for CakePHP. Link to article discussing its use in Cake 1.2: http://www.pagebakers.nl/2007/06/05/using-json-in-cakephp-12/

Upvotes: 1

brettkelly
brettkelly

Reputation: 28205

If you have PHP 5.2 or higher, it ships with JSON encode/decode support. Check the docs here.

You'll probably need to do the encoding/output by hand, but it should be trivial to code.

Bonus points would be to build it as a behavior :)

Edit:

Check out the $javascript->object() method here, it may do what you want.

Upvotes: 2

Related Questions