Simple AJAX / JSON response with CakePHP

14,781

Solution 1

I don't know what you read but I guess it was not the official documentation. The official documentation contains examples how to do it.

class PostsController extends AppController {
    public $components = array('RequestHandler');

    public function index() {
        // some code that created $posts and $comments
        $this->set(compact('posts', 'comments'));
        $this->set('_serialize', array('posts', 'comments'));
    }
}

If the action is called with the .json extension you get json back, if its called with .xml you'll get xml back.

If you want or need to you can still create view files. Its as well explained on that page.

// Controller code
class PostsController extends AppController {
    public function index() {
        $this->set(compact('posts', 'comments'));
    }
}

// View code - app/View/Posts/json/index.ctp
foreach ($posts as &$post) {
    unset($post['Post']['generated_html']);
}
echo json_encode(compact('posts', 'comments'));

Solution 2

// Controller
public function listAll() {
    $myModel = $this->MyModel->find('all');

    if($this->request->is('ajax') {
        $this->layout=null;
        // What else?
        echo json_encode($myModel);
        exit;
        // What else?

    }
}

You must use exit after the echo and you are already using layout null so that is OK.

You do not have to use View for this, and it is your wish to work with components. Well all you can do from controller itself and there is nothing wrong with it!

Iinjoy

Share:
14,781
Christopher Francisco
Author by

Christopher Francisco

Updated on June 04, 2022

Comments

  • Christopher Francisco
    Christopher Francisco almost 2 years

    I'm new to cakePHP. Needless to say I don't know where to start reading. Read several pages about AJAX and JSON responses and all I could understand is that somehow I need to use Router::parseExtensions() and RequestHandlerComponent, but none had a sample code I could read.

    What I need is to call function MyController::listAll() and return a Model::find('all') in JSON format so I can use it with JS.

    Do I need a View for this? In what folder should that view go? What extension should it have? Where do I put the Router::parseExtension() and RequestHandlerComponent?

    // Controller
    public function listAll() {
        $myModel = $this->MyModel->find('all');
    
        if($this->request->is('ajax') {
            $this->layout=null;
    
            // What else?
    
        }
    }
    
  • Synexis
    Synexis over 9 years
    I find this much more straightforward than Cake's built-in methods, however exiting within an action may prevent any after filters or shutdown methods. Instead of "exit" you can add the following: "$this->layout = false" and "$this->render(false)" for the same effect without prematurely ending the script.
  • I Wanna Know
    I Wanna Know over 9 years
    Ok, but then how to use this response you've serialized? It becomes a xmlhttp.responseText? My question: stackoverflow.com/questions/27979813/… Please!
  • I Wanna Know
    I Wanna Know over 9 years