2013-07-31 72 views
1

我正在編寫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 

爲什麼頭和數據沒有改變?我該如何解決它?

回答

1

看起來像_init()方法根本沒有執行。嘗試將其重命名爲init()

+0

謝謝,問題是在下劃線。 – Tamara

0

,因爲我覺得你的代碼已經丟失了一些步驟調用上下文切換幫手

這裏工作示例我使用的上下文切換的REST API

只是去扔this API example,使你的腳本工作

希望這會幫助你。