我通常将一个编码的 json 对象附加到响应主体,但是我现在遇到了需要使用 ContextSwitch 操作助手的情况。
我有一个需要三种不同响应上下文的 Zend_Form:
- html - 将表单呈现为普通 html 在布局中。
- html-partial - 呈现的 ajax“获取”请求 只是 html 形式。
- json - 返回的 ajax“发布”请求 任何表单验证错误消息。
对于每个上下文,我有 3 个 View 脚本。虽然这两个 html 上下文可以使用相同的 View 脚本,但我还没有想出这是否可能。
- form.phtml
- form.html.phtml
- form.json.phtml
html 上下文 View 工作正常,但未拾取 json View 。覆盖默认 json post 回调行为或将自定义编码对象传递到响应正文的最佳方法是什么?
最佳答案
就我个人而言,我不使用“View”来生成 JSON 内容。
在我的 init()
中,我有这样的东西:
$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->setAutoJsonSerialization(false)
->addActionContext('index', array('html', 'json'))
->initContext();
在我的 indexAction()
中:
if ( true === $this->isAjaxJson() ) {
$this->_helper->json(
array(
'response' => $myResponse,
'message' => $myMesage
)
);
return;
}
希望这对您有所帮助。
关于php - 使用 Zend Action Helper ContextSwitch 创建自定义 JSON 响应对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5231584/