Simple AJAX / JSON response with CakePHP

2019-04-14 07:06发布

问题:

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?

    }
}

回答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'));


回答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



回答3:

In Cakephp 3.5 you can send json response as below:

//in the controller

public function XYZ() {
            $this->viewBuilder()->setlayout(null);
            $this->autoRender = false;

            $taskData = $this->_getTaskData();
            $data = $this->XYZ->getAllEventsById( $taskData['tenderId']);

            $this->response->type('json');
            $this->response->body(json_encode($data));
            return $this->response;
}



回答4:

Try this:

public function listAll(){

    $this->autoRender=false;

    $output = $this->MyModel->find('all')->toArray();

    $this->response = $this->response->withType('json');
    $json = json_encode($output);
    $this->response = $this->response->withStringBody($json);

 }