I’m having a problem with rendering json in my app. It was working fine earlier, and I don’t think I changed the relevant code, but I must have changed something else which caused this to happen!
(I’m just getting to grips with Cake 3, having used Cake 2 for a long time, so I’m also vaguely aware that there’s maybe a better way to handle requests and return json, but I haven’t figured that out yet.)
Basically, I’m trying to render a JSON response. In my NewsController, I have the following (in a function called imageUpload):
$this->viewBuilder()->layout('json');
This layout is basically blank (just contains <?= $this->fetch('content') ?>).
The function then does what it needs to do, and finally attempts to render the result like this:
$result = array('success' => 1, 'filename' => $filename);
$this->set('result', json_encode($result));
$this->render('json_result');
As I said, this used to work, but now I’m getting the following error message:
Template file "Admin\News\json\json_result.ctp" is missing.
The file json_result.ctp is, and has always been, in the folder “Admin\News\json_result.ctp” (i.e. there’s never been a ‘json’ subfolder to contain the file).
Obviously I could fix this by creating the json subfolder and moving the file into it, but it’s being used in other places and I don’t want to break those, plus I don’t like to do things like that without understanding why!
Can anyone shed any light on this?
Thanks!