【发布时间】:2013-07-31 08:58:45
【问题描述】:
我正在编写 REST api,并希望以 JSON 格式返回响应。因此,我阅读了有关“ContextSwitch”的信息。但我无法将标头更改为“application/json”并将数据转换为 json。
这是我的控制器的代码:
public function preDispatch()
{
$this->getRequest()->setParam('format', 'json');
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
}
public function _init()
{
$contextSwitch = $this->_helper->getHelper('contextSwitch');
$contextSwitch ->addActionContext('post', 'json')
->initContext('json');
}
public function postAction()
{
echo 'test';
}
当我使用 Curl 命令行工具检查响应时,我收到了:
< Content-Length: 4
< Content-Type: text/html
<
* Connection #0 to host localhost left intact
test* Closing connection #0
为什么标题和数据没有改变?我该如何解决?
【问题讨论】:
标签: zend-framework context-switch