【问题标题】:Zend Framework 1.12 and 'ContextSwitch' helperZend Framework 1.12 和 'ContextSwitch' 助手
【发布时间】: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


    【解决方案1】:

    看起来_init() 方法根本没有执行。尝试将其重命名为init()

    【讨论】:

    • 谢谢,问题出在下划线处。
    【解决方案2】:

    我认为您的代码缺少一些调用上下文切换助手的步骤

    这里是我使用 REST API 的上下文切换的工作示例

    只需抛出 this API example 并让您的脚本正常工作

    希望这对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多