4

I've been trying to setup an Ajax Call in Cakephp 3.0.11. I've followed the explanation here : http://book.cakephp.org/3.0/en/views/json-and-xml-views.html

Json enabled in routing (but i'm not really sure that's usefull) :

$routes->extensions(['json', 'xml', 'html']);

I've setup my exemple in controller :

$returnObject = new ObjectReturn();
$this->set('returnObject', $returnObject);
$this->set('_serialize', ['returnObject']);

But when I make my ajax call, I've got :

{
    "message": "Template file \Pages\\score.ctp\ is missing.",
    "url": "\/pages\/score",
    "code": 500
}

If I create the page, he juste render me some html, with default.ctp as a layout. What's wrong here ?

Thanks a lot !

Holt
  • 36,600
  • 7
  • 92
  • 139
Gael.D
  • 500
  • 1
  • 5
  • 16
  • Did you load the `RequestHandlerComponent` in your controller as explained [here](http://book.cakephp.org/3.0/en/views/json-and-xml-views.html#enabling-data-views-in-your-application)? – Holt Aug 19 '15 at 07:30
  • And b.t.w, to enable `json` view using extensions and request handling, your url needs to use the `.json` extension, such as `/pages/score.json`. – Holt Aug 19 '15 at 07:36
  • Hello Thanks for the answer. I've upgraded to the last version of Cake, and now it works. Maybe a bug have been correct. Thanks for your answer ! – Gael.D Aug 19 '15 at 13:42
  • 1
    This worked when you upgraded because of the following: `As of 3.1.0 AppController, in the application skeleton automatically adds '_serialize' => true to all XML/JSON requests. You will need to remove this code from the beforeRender callback if you want to use view files.` from http://book.cakephp.org/3.0/en/views/json-and-xml-views.html#using-a-data-view-with-template-files – Steve Tauber Nov 16 '15 at 14:30

2 Answers2

9

If you'd like to force every response (except errors) to JSON (and skip templates) you can put this code in your AppController.php

public function initialize()
{
    parent::initialize();
    $this->loadComponent('RequestHandler');
}

public function beforeRender(Event $event)
{
    $this->RequestHandler->renderAs($this, 'json');
    $this->response->type('application/json');
    $this->set('_serialize', true);
}

This loads the RequestHandlerComponent, forces the render to JSON, forces the response type to JSON, and skips using templates.

More in the manual here: http://book.cakephp.org/3.0/en/controllers/components/request-handling.html

Steve Tauber
  • 9,551
  • 5
  • 42
  • 46
  • 1
    Thank you so much!! I was driving crazy to output json!! Seems like setting `$this->set('_serialize', $vars);` is not enough!! Tip: works also for CakePHP 2! – Martin_Lakes Dec 03 '19 at 20:30
8

To achieve a json/xml render view in your cakephp 3 application, you need to edit two file in your application

  1. AppController.php

Edit initialize function to look like this

public function initialize() {
    parent::initialize();
    $this->loadComponent('RequestHandler');/*This line loads the required RequestHandler Component*/
}
  1. routes.php (under your config folder)

Edit Router::scope() to look like this

Router::scope('/', function ($routes) {
    $routes->extensions(['json', 'xml', 'ajax']);
    /*Other route definitions as already existing*/
}

You may now load a json view by adding .json or xml by adding .xml to the link that normally loads your html.

Cheers

Ayo Makanjuola
  • 608
  • 7
  • 14