When we need to call the same wrapper function in the control layer, we write the same function several times, it seems that the code is very rambling and not streamlined;
TP Framework has a good mechanism, can be common define a function.php function, when we call the control layer when the direct call can be used, convenient and fast;
The specific implementation of the following is convenient:
We can set up a public function in three more places function.php
(Note: function.php default is not present, need to create manually)
- Root directory \application\common\common\function.php (public functions, home and admin can all be called)
- Root directory \application\home\common\function.php (home layer public function, can only be called by the home layer)
- Root directory \application\admin\common\function.php (admin layer public function, can only be called by admin layer)
Specify where function is stored and where you can choose freely
It is also possible when we do not want to use function-named function.php functions, but it is necessary to manually set the
Here's how:
To add a configuration to the root directory/application/common/conf/config.php:
"Load_ext_file" = ' vaildata ',//
The vaildate.php file can be loaded automatically at this point.
If there are multiple files that need to be loaded automatically, separate the values of the configuration items with ",", for example:
"Load_ext_file" = ' vaildata,upload,download ',//
Example: encapsulating the Curl function to get interface data
First step: Create the function.php function in the root directory \application\home\common\;
<?PHP//encapsulating the common curl function functionCurl$url){ $curl=Curl_init (); curl_setopt ($curl, Curlopt_url,$url); curl_setopt ($curl, Curlopt_returntransfer, 1); curl_setopt ($curl, Curlopt_ssl_verifypeer,false); $data= Curl_exec ($curl); Curl_close ($curl); return $data; } ?>
Step Two: Call the Curl function on the controller layer that needs to be called;
<?phpnamespace Home\controller; UseThink\controller;classWxjsapicontrollerextendsController { Public functionindex () {//Get Interface Data $url= ' Fill in the interface address ' here; $data _son= Curl ($url);//Adjust the Curl function in the function.php package $data= Json_decode ($data _json,true) [' Access_token ']; Var_dump($data);//JSON data Conversion array Form//If you also need to call the Curl function, the same as the direct curl ("interface address") can be;}}? >
The above is an example of the Thinkphp3.2.3 framework to encapsulate the common functions and encapsulate the curl function to obtain the interface data;
Thinkphp3.2.3 a common function in the framework, such as encapsulating the Curl function to get interface data