Hey, I handle my rest API in a similar way. I have BaseController which other controllers extend. In the BaseController I have the following:
// Return the API response.
public function afterExecuteRoute()
{
// Status code & Response header
$this->response->setStatusCode($this->_response['statusCode'], $this->_statusCodes[$this->_response['statusCode']]);
$this->response->setHeader('Access-Control-Allow-Origin', '*');
// Allow user to choose content type. Defaults to JSON
if ($this->request->getHeader('Accept') == 'application/xml') {
$this->response->setContentType('application/xml', 'UTF-8');
// @TODO: generate XML from array
d($this->_response);
} else {
$this->response->setContentType('application/json', 'UTF-8');
return $this->response->setJsonContent($this->_response)->send();
}
}
So in every controller I just pass data to $this->_response
and afterExecuteRoute in my BaseController does the rest.
It has to work for you too, but as @le51 asked what is your actual code?