在为app开发接口过程中,我们必不可少的要为app前端工程师们提供返回的数据,如何灵活快速又易懂的返回他们需要的数据是非常关键的。
其实thinkphp已经把很多我们要用到的都写出来了,我们只需要稍作修改即可灵活的返回我们需要的数据。
首先,修改Thinkphp/Library/Controller.class.php 在其中增加一个函数 apiReturn();
/**
* [apiReturn 用于给app提供接口使用 带有请求结果状态表示,和结果提示,默认返回json]
* @param [number] $status [请求结果的状态标识,设定后要在文档中给予说明]
* @param string $message [请求结果的提示语句]
* @param [array] $data [请求返回的数据,app前端需要的数据]
* @param [string] $type [要返回的数据类型,支持json,xml,默认返回json]
* @return [json或xml] [返回数据]
*/
protected function apiReturn($status,$message='',$data,$type){
if(!is_numeric($status) || !is_string($message) ){
$this->apiReturn('400','参数错误');
}
$res = array();
$res['status'] = $status;
$res['message'] = $message;
$res['data'] = $data;
if(in_array($type, array('json','xml'))){
$this->ajaxReturn($res,$type);
}else{
$this->ajaxReturn($res);
}
}
增加了这样一个函数后,我们就可以很轻松的在任意控制器下使用了
举个例子 在thinkphp的Home分组下的Index控制器里我们新增一个test方法
<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
public function index(){
$this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>!</p><br/>[ 您现在访问的是Home模块的Index控制器 ]</div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
}
public function test(){
$data = array(
'id'=>2,
'username'=>'明之暗夜',
'info'=>array('age'=>24,'address'=>'学府路','url'=>'http://cnblogs.com/dmm888')
);
if($data){
$this->apiReturn(200,'读取用户信息成功',$data);
}
}
}
在浏览器访问这个接口时可以看到返回的信息
{"status":200,"message":"\u8bfb\u53d6\u7528\u6237\u4fe1\u606f\u6210\u529f","data":{"id":2,"username":"\u660e\u4e4b\u6697\u591c","info":{"age":24,"address":"\u5b66\u5e9c\u8def","url":"http:\/\/cnblogs.com\/dmm888"}}}
这里的数字200可以自定义的 但是我们要在接口说明文档中给予说明。
要返回xml只要在后面添加参数就可以
<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
public function index(){
$this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>!</p><br/>[ 您现在访问的是Home模块的Index控制器 ]</div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
}
public function test(){
$data = array(
'id'=>2,
'username'=>'明之暗夜',
'info'=>array('age'=>24,'address'=>'学府路','url'=>'http://cnblogs.com/dmm888')
);
if($data){
$this->apiReturn(200,'读取用户信息成功',$data,xml);
}
}
}
返回的数据如下
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<think>
<status>200</status>
<message>读取用户信息成功</message>
<data>
<id>2</id>
<username>明之暗夜</username>
<info>
<age>24</age>
<address>学府路</address>
<url>http://cnblogs.com/dmm888</url>
</info>
</data>
</think>
发表评论 取消回复