在為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>
還有很多可以拓展的 就不一一列舉了 如果有什么好的意見可以直接給我提