在 Joomla 框架中设置自定义内容类型
Set custom Content-Type in Joomla Framework
我正在使用 Joomla 框架 API 构建 Web 应用程序。
现在我想以 JSON 格式显示数据库中的一些数据,我必须将默认的 HTTP-Content-Type 从 text/html 更改为 application/json。
Joomla 框架附带了 Symfony HTTP 基础组件,所以我在网上搜索并找到了这个:
use Symfony\Component\HttpFoundation\Response;
$response = new Response();
$response->headers->set('Content-Type', 'application/json');
$response->send();
但是没有效果。
我也试过标准方式:
header("Content-Type: application/json");
那也不行..
感谢任何建议!
检查 JApplicationWeb API 和 setHeader / sendHeader 函数。
我找到了以下解决方案:
在\Joomla\Application\AbstractWebApplication中我写了一个设置现有属性的新方法mimeType:
public function setMimeType($mimeType) {
$this->mimeType = $mimeType;
}
这非常有效。
我正在使用 Joomla 框架 API 构建 Web 应用程序。 现在我想以 JSON 格式显示数据库中的一些数据,我必须将默认的 HTTP-Content-Type 从 text/html 更改为 application/json。 Joomla 框架附带了 Symfony HTTP 基础组件,所以我在网上搜索并找到了这个:
use Symfony\Component\HttpFoundation\Response;
$response = new Response();
$response->headers->set('Content-Type', 'application/json');
$response->send();
但是没有效果。
我也试过标准方式:
header("Content-Type: application/json");
那也不行..
感谢任何建议!
检查 JApplicationWeb API 和 setHeader / sendHeader 函数。
我找到了以下解决方案:
在\Joomla\Application\AbstractWebApplication中我写了一个设置现有属性的新方法mimeType:
public function setMimeType($mimeType) {
$this->mimeType = $mimeType;
}
这非常有效。