You are skipping the whole View part of the MVC framework with what you are doing.
So you disable the auto-rendering part and manually do the view rendering part in your controller which violates the MVC paradigm
We recommend you use the JSON view or in your specific case (because you are on such an old CakePHP version) go with this JSON view doc since you still have to use the old RequestHandler component.
Iām not super familiar with all those many conventions, but as for MVC:
Iāve got some data-base related definitions in Model, Iāve got some queries in Controller and I pass it to Template via $this->set, which template is mostly in php and html, which then goes from apache to my browser in simple case via old good known get request. That is working form me and I think I understand it.
And now if I want to use some other kind o view, I need some JS which will handle all data transfer and rendering? Am I right?
controllers are called from requests to fetch data via the models and then pass it on to the
view which is responsible to render the previously fetched (and maybe transformed) data from the controller.
In your case you say you pass on query objects from the controller to the view via $this->set which is 100% true.
BUT you need to understand that CakePHP does its magic when you iterate over that query inside the template that it actually executes the query and transforms the query object into an array of entities.
How this all connects together with a JSON endpoint will be explained in my next video which will come up very soon.
I still donāt get how it is rendered at browser side. Letās say I manage to send JSON from my server one way or another, and then what? Is there some built mechanics in cake which will receive this at browser site and render somehow? Iām only use JS XMLHttpRequest as helpers/addition, for example to find some stuff while typing, without sending whole form back and forth. And for this purpouse making separate method in Controller which receive this requests and send back results in JSON via echo json_encode($query->toArray()); works fine, and seems to me as an easiest way.
CakePHP comes with a very barebone HTML and CSS setup where you are required to built your frontend however you like. This also means, that there is no JS logic whatsoever to automatically load/render JSON APIs. You have to build that yourself (just like you manually added those AJAX/XMLHttpRequest calls in your JS files).