mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 15:12:48 +08:00
Controller使用 Transform 编码 JSON 和 xml 数据
This commit is contained in:
@@ -85,16 +85,16 @@ class Controller {
|
|||||||
case 'JSON':
|
case 'JSON':
|
||||||
// 返回JSON数据格式到客户端 包含状态信息
|
// 返回JSON数据格式到客户端 包含状态信息
|
||||||
header('Content-Type:application/json; charset=utf-8');
|
header('Content-Type:application/json; charset=utf-8');
|
||||||
exit(json_encode($data));
|
exit(Think\Transform::jsonEncode($data));
|
||||||
case 'XML':
|
case 'XML':
|
||||||
// 返回xml格式数据
|
// 返回xml格式数据
|
||||||
header('Content-Type:text/xml; charset=utf-8');
|
header('Content-Type:text/xml; charset=utf-8');
|
||||||
exit(xml_encode($data));
|
exit(Think\Transform::xmlEncode($data));
|
||||||
case 'JSONP':
|
case 'JSONP':
|
||||||
// 返回JSON数据格式到客户端 包含状态信息
|
// 返回JSON数据格式到客户端 包含状态信息
|
||||||
header('Content-Type:application/javascript; charset=utf-8');
|
header('Content-Type:application/javascript; charset=utf-8');
|
||||||
$handler = isset($_GET[C('var_jsonp_handler')]) ? $_GET[C('var_jsonp_handler')] : C('default_jsonp_handler');
|
$handler = isset($_GET[C('var_jsonp_handler')]) ? $_GET[C('var_jsonp_handler')] : C('default_jsonp_handler');
|
||||||
exit($handler . '(' . json_encode($data) . ');');
|
exit($handler . '(' . Think\Transform::jsonEncode($data) . ');');
|
||||||
case 'SCRIPT':
|
case 'SCRIPT':
|
||||||
// 返回可执行的js脚本
|
// 返回可执行的js脚本
|
||||||
header('Content-Type:application/javascript; charset=utf-8');
|
header('Content-Type:application/javascript; charset=utf-8');
|
||||||
|
|||||||
@@ -13,9 +13,14 @@ namespace Think;
|
|||||||
|
|
||||||
// 内容解析类
|
// 内容解析类
|
||||||
class Transform {
|
class Transform {
|
||||||
|
|
||||||
static private $handler = [];
|
static private $handler = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化解析驱动
|
||||||
|
* @static
|
||||||
|
* @access private
|
||||||
|
* @param string $type 驱动类型
|
||||||
|
*/
|
||||||
static private function init($type){
|
static private function init($type){
|
||||||
if(!isset(self::$handler[$type])) {
|
if(!isset(self::$handler[$type])) {
|
||||||
$class = '\\Think\\Transform\\Driver\\' . ucwords($type);
|
$class = '\\Think\\Transform\\Driver\\' . ucwords($type);
|
||||||
@@ -23,13 +28,26 @@ class Transform {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 编码内容
|
/**
|
||||||
|
* 编码内容
|
||||||
|
* @static
|
||||||
|
* @access public
|
||||||
|
* @param mixed $content 要编码的数据
|
||||||
|
* @param string $type 数据类型
|
||||||
|
* @return string 编码后的数据
|
||||||
|
*/
|
||||||
static public function encode($content, $type){
|
static public function encode($content, $type){
|
||||||
self::init($type);
|
self::init($type);
|
||||||
return self::$handler[$type]->encode($content);
|
return self::$handler[$type]->encode($content);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 解码内容
|
/**
|
||||||
|
* 解码数据
|
||||||
|
* @param string $content 要解码的数据
|
||||||
|
* @param string $type 数据类型
|
||||||
|
* @param boolean $assoc 是否返回数组
|
||||||
|
* @return mixed 解码后的数据
|
||||||
|
*/
|
||||||
static public function decode($content, $type, $assoc = true){
|
static public function decode($content, $type, $assoc = true){
|
||||||
self::init($type);
|
self::init($type);
|
||||||
return self::$handler[$type]->decode($content, $assoc);
|
return self::$handler[$type]->decode($content, $assoc);
|
||||||
|
|||||||
Reference in New Issue
Block a user