zen of coding

JSON output with CakePHP

Update (2/14/2011): Take a look at a more robust approach that works with recent 1.3 versions of CakePHP.

Update (9/25/2008): Since the writing of this post, the CakePHP manual has been updated with information on using the RequstHandler, which is an excellent supplement to the approach described below. You should definitely familiarize yourself with the way it works:


If you are doing some AJAX development there is a very good chance that you’d like to output your data in a JSON format.

Let’s say that we would like to output all of our user’s data (User model) as JSON. Well, like many other things, it couldn’t be easier with CakePHP.

Let’s create a new action called viewAllJson in our users controller…

function viewAllJson() {
  $this->layout = 'ajax';
  $users = $this->User->find('all', array('recursive' => -1));

This should be pretty much self explanatory. You are using the default AJAX layout, which should be just a blank page, since all you need is the output of the JSON data (no other HTML/text should be present). You are finding all Users and then setting the resulting array for the view. Note, that I’m using the Bindable behavior, hence the use of the ‘restrict’ key (you might need to change that if you don’t use Bindable).

Now we need to create a view (view_all_json.ctp) so that we can display the JSON data.

Configure::write('debug', 0);
echo json_encode($users);

… and really that’s all there is to it.

  • If you use RequestHandler component, there is no need of calling the Ajax layout, it will be automagically loaded in every ajax call.

  • teknoid

    @ Martin Bavio

    Thanks, good point.

  • I always put my debug in 0 into core, thanks for this tip.

  • teknoid

    @Webmaster Mexico

    Good point regarding debug 0, but for testing or in your development this does the trick and makes your code easily protable from dev to test to production without any changes.

  • So it does not need any content header like Javascript set in the controller like this?
    $this->RequestHandler->setContent(‘json’, ‘text/javascript’);

  • teknoid


    Usually you won’t need to set a header. Your JS library should deal with the response correctly (in most cases, at least).

  • Jason Cleeland

    I’ve been looking for something that explains how to do this simply and directly for hours and hours. Thank you so incredibly much, I can now attempt to return my hair to its rightful place.

    As someone who is relatively new to CakePHP, I have to say that it really is simple.. when you know what to do. It’s the bit between not knowing, and knowing that’s really hard. Posts like this really help.

  • teknoid

    @Jason Cleeland

    You should also take a look at the RequestHandler component, to ease some of the tasks…


  • Anish


    Thank you for your wonderful tutorial.

    But when I tried it in my application, I got the following error

    Error: The action json_view is not defined in controller KeywordsController

    I ve already set the Router::parseExtensions(‘rss’, ‘json’, ‘xml’); and also put var $components = array(‘RequestHandler’); in my handler.

    Thanks in Advance

    • Anish


      No need, I forgot to add helper.

      Sorry If I bothered you.



  • @Anish

    No problem. Glad you’ve got it figured out.

  • abdollah2

    thanks for this tip

  • Manoj

    Hi my name is manoj i am using cake 1.3.
    In cake 1.3 how to take a json value in a function from http post request

  • thanks a lot…..great pice of useful info!!!

%d bloggers like this: