Home  >  Article  >  php教程  >  Zend Framework动作助手Json用法实例分析,zendjson

Zend Framework动作助手Json用法实例分析,zendjson

WBOY
WBOYOriginal
2016-06-13 08:44:551209browse

Zend Framework动作助手Json用法实例分析,zendjson

本文实例讲述了Zend Framework动作助手Json用法。分享给大家供大家参考,具体如下:

Json的使用较简单,以下是文档给出的用法:

Json 用来解码和发送 JSON 响应;

当处理期望数据表响应的 AJAX 请求,JSON 响应迅速变成选择的响应。

JSON 可以立即在客户端被解析,从而快速执行。

JSON 动作助手完成以下任务:

如果布局是打开(enabled)的,则关闭(disable)它。

如果视图解析器(ViewRenderer)是打开的,则关闭它。

设置 'Content-Type' 响应头为 'application/json'。

缺省地,不需要等待动作执行完成,立即返回响应。

用法很简单:或者把它作为助手代理的方法来调用,或者调用 encodeJson() 和 sendJson() 方法的其中之一:

class FooController extends Zend_Controller_Action
{
  public function barAction()
  {
    // do some processing...
    // Send the JSON response:
    $this->_helper->json($data);
    // or...
    $this->_helper->json->sendJson($data);
    // or retrieve the json:
    $json = $this->_helper->json->encodeJson($data);
  }
}

Note: 保持布局 (Keeping Layouts)

如果你为 JSON 响应有分离的布局 - 也许把 JSON 封装到一些上下文 - 在 JSON 助手的每个方法接受第二个可选的参数:打开或关闭布局的 flag ,传递一个布尔 true 值将使布局保持打开:

class FooController extends Zend_Controller_Action
{
  public function barAction()
  {
    // Retrieve the json, keeping layouts:
    $json = $this->_helper->json->encodeJson($data, true);
  }
}

更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

您可能感兴趣的文章:

  • Zend Framework教程之MVC框架的Controller用法分析
  • Zend Framework教程之路由功能Zend_Controller_Router详解
  • Zend Framework教程之Zend_Controller_Plugin插件用法详解
  • Zend Framework教程之响应对象的封装Zend_Controller_Response实例详解
  • Zend Framework教程之请求对象的封装Zend_Controller_Request实例详解
  • Zend Framework教程之动作的基类Zend_Controller_Action详解
  • Zend Framework教程之分发器Zend_Controller_Dispatcher用法详解
  • Zend Framework教程之前端控制器Zend_Controller_Front用法详解
  • Zend Framework动作助手Redirector用法实例详解
  • Zend Framework动作助手Url用法详解
  • Zend Framework动作助手FlashMessenger用法详解
  • Zend Framework教程之Resource Autoloading用法实例
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn