thinkphp encapsulation of communication data during the development of the app interface

Source: Internet
Author: User
Tags ming

In the process of developing an interface for an app, it is essential that we provide the back-up data for the app front-end engineers, and that it is critical to be flexible and quick and easy to understand how to return the data they need.

In fact, Thinkphp has already put a lot of what we need to write, we only need a little change can be flexible to return the data we need.

First, modify the thinkphp/library/controller.class.php in which a function apireturn () is added;

 /** [Apireturn is used to provide an interface to the app with a request result status indication, and a result hint, which returns JSON by default] * @param [number] $status [status identification of the request result, instructions in the document after setting] * @param string $message [Prompt statement requesting result] * @param [array] $data [data requested by the app front end required] * @param [string] $type [data type to return, support json,x ML, return JSON by default] * @return [JSON or XML] [return data]*/    protected functionApireturn ($status,$message= ",$data,$type){        if(!Is_numeric($status) || !is_string($message) ){            $this->apireturn (' 400 ', ' parameter error ')); }        $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); }            }

By adding such a function, we can easily use it under any controller.

For example, we added a test method to the index controller under the thinkphp's home group.

<?phpnamespace Home\controller; UseThink\controller;classIndexcontrollerextendsController { Public functionindex () {$this->show (' <style type= "text/css" >*{padding:0; margin:0;} div{padding:4px 48px;} body{background: #fff; font- Family: "Microsoft Jas Black"; 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;" > ); }     Public functionTest () {$data=Array(            ' id ' =>2, ' username ' = ' The Night of the Ming ', ' info ' =Array(' Age ' =>24, ' address ' = ' Academy road ', ' url ' = ' http://cnblogs.com/dmm888 ')        ); if($data){            $this->apireturn (200, ' Read user information success ',$data); }    }    }

You can see the information returned when the browser accesses this interface

{"Status": $, "message": "\u8bfb\u53d6\u7528\u6237\u4fe1\u606f\u6210\u529f", "data": {"id": 2, "username": "\u660e\ U4e4b\u6697\u591c "," info ": {" Age ":" Address ":" \u5b66\u5e9c\u8def "," url ":" Http:\/\/cnblogs.com\/dmm888 "}}}

The number 200 here can be customized, but we want to give instructions in the interface documentation.

To return XML, simply add a parameter later to

<?phpnamespace Home\controller; UseThink\controller;classIndexcontrollerextendsController { Public functionindex () {$this->show (' <style type= "text/css" >*{padding:0; margin:0;} div{padding:4px 48px;} body{background: #fff; font- Family: "Microsoft Jas Black"; 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;" > ); }     Public functionTest () {$data=Array(            ' id ' =>2, ' username ' = ' The Night of the Ming ', ' info ' =Array(' Age ' =>24, ' address ' = ' Academy road ', ' url ' = ' http://cnblogs.com/dmm888 ')        ); if($data){            $this->apireturn (200, ' Read user information success ',$data,XML); }    }    }

The returned data is as follows

This XML file does does appear to has any style information associated with it. The document tree is shown below.<Think><Status>200</Status><message>Read user information successfully</message><Data><ID>2</ID><username>The night of the Ming</username><Info>< Age>24</ Age><Address>Xue Lu</Address><URL>http://cnblogs.com/dmm888</URL></Info></Data></Think>

There are a lot of things that can be expanded not to mention that if there is any good advice can be directly to me

thinkphp encapsulation of communication data during the development of the app interface

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.