I have started building a web application using the Joomla Framework. It consists of a simple web page which will be displayed in an iFrame along with an API that will be used to update the content with AJAX.
I followed the Getting Started with Composer and Joomla! article and have the Framework App up and running. I have customised the default HTML output to work for my iFrame and have made a start on the API. I used News as a template and created a controller, model, table and view for my API which all works great. I then created a very simple Twig template which returns a JSON representation of my data like this:
{{ items|json_encode|raw }}
So far so good but I get this header returned which will cause problems when using AJAX with jQuery:
Content-Type:text/html; charset=utf-8
I thought it would be a simple case of adding this but no matter where I put it, it doesn't change the header that is returned:
$this->app->setHeader('Content-Type', 'application/json', true);
I know that setHeader is working as if I change the name, I see the new header returned so I assume that it is being overwritten later in the execution.
So my question is, am I going about this the right way? My view extends DefaultHtmlView which feels wrong, should I create a DefaultJsonView? How can I set the correct Content-Type or is there a better approach for returning JSON?
Any help of advice that can be given about the best way to use the Joomla Framework to build an API will be greatly appreciated.