我正在編寫REST API,並希望以JSON格式返回響應。所以,我讀了關於'ContextSwitch'的內容。但我不能讓它改變頭到'application/json'並將數據轉換成json。Zend Framework 1.12和'ContextSwitch'幫手
這裏是我的控制器代碼:
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
爲什麼頭和數據沒有改變?我該如何解決它?
謝謝,問題是在下劃線。 – Tamara