Rendering suddenly throwing up "Missing Template" issue


#1

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!


#2

Hi,
I don’t have an in-depth answer for you - but I faced similar situation with Ajax responses. In my projects I simply create the ajax folder and put a blank ctp file matching the name of the method in there - and so far life has been easy :wink: Nothing broke… that’s for sure.

I’d say, please go ahead and create the json folder and move the file under it.

Cheers.


#3

Access the url using the “.json” extension

/YourController/yourAction.json.


#4

True - you should be accessing using the .json extension to trigger the related view.