2

I am using the Joomla framework API to build a web application. Now I want to display some data from the database in JSON format and I have to change the default HTTP-Content-Type from text/html to application/json. The Joomla framework comes with the Symfony HTTP foundation component, so I've searched the web and found this:

use Symfony\Component\HttpFoundation\Response;
$response = new Response();
$response->headers->set('Content-Type', 'application/json');
$response->send();

But there is no effect.

I have also tried the standard way:

header("Content-Type: application/json");

That doesn't work too..

Thanks for any advice!

nicl
  • 41
  • 4
  • AFAIK the only Symfony component used in Joomla is the YAML component. Where did you read that "Joomla framework comes with Symfony"? – Francesco Abeni Jul 10 '15 at 16:11
  • When I installed the framework via composer, it installed the Symfony Http foundation component too.. – nicl Jul 11 '15 at 09:57
  • Looking at https://packagist.org/packages/joomla/framework, it seems that the only dependencies installed are psr/log and symfony/yaml. I tried myself and I did not get Symfony Http Foundation component. Maybe it's a different version you installed? P.S. sorry for insisting on this point, but I think it's quite relevant to your question. – Francesco Abeni Jul 11 '15 at 14:51
  • I used the Joomla framework sample application, that installed some other things, including the Symfony Http Foundation component. Look at [this file](https://github.com/dbhurley/framework-app/blob/master/composer.json) – nicl Jul 12 '15 at 11:39
  • Ok. That's a custom application, like the one you are trying to build. In your application you can include whatever component you want, but the Joomla Framework itself does NOT come with Symfony Http Foundation. – Francesco Abeni Jul 12 '15 at 12:00

2 Answers2

1

I found the following solution:

In \Joomla\Application\AbstractWebApplication I wrote a new method that sets the existing attribute mimeType:

public function setMimeType($mimeType) {
            $this->mimeType = $mimeType;
        }

This works perfectly.

nicl
  • 41
  • 4
0

Check the JApplicationWeb API and the setHeader / sendHeader functions.