This article describes how to encapsulate public functions under the TP framework. If you don’t know about encapsulating public functions under the TP framework or are interested in encapsulating public functions under the TP framework, then let’s take a look at this article. , Okay, let’s stop talking and get to the point
When we need to call the same encapsulated function in the control layer, writing the same function multiple times makes the code very complicated and not streamlined;
The TP framework has a very good mechanism. You can define a function in Common.php function can be used by calling it directly when we call it in the control layer, which is convenient and fast;
Detailed implementation The convenience is as follows:
We can set public functions in three places function.php
(Note: function.php does not exist by default and needs to be manually Create)
Root directory\Application\Common\Common\function.php (Public functions can be called by Home and Admin)
Root directory\Application\Home\Common\function.php (functions common to Home layer, can only be called by Home layer)
Root directory\Application\Admin\Common\function.php (functions common to the Admin layer can only be called by the Admin layer)
Specifically speaking, you can freely choose where the function is stored
In addition, when we don’t want to use the function.php function named function, it is also possible, but that’s You need to manually set it yourself
The method is as follows:
Add configuration in the root directory/Application/Common/Conf/config.php:
"LOAD_EXT_FILE" =>'vaildata',//自动加载公共目录Common下的扩展函数库
The vaildate.php file can be automatically loaded at this time.
If there are multiple files that need to be loaded automatically, just separate them with "," in the value of the configuration item, for example:
"LOAD_EXT_FILE" =>'vaildata,upload,download',//自动加载公共目录Common下的扩展函数库
Example: Encapsulate CURL function to obtain interface data
Step 1: In the root directory\Application\Home\ Common\Create function.php function;
<?php //封装公共的CURL函数 function curl($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 2: Call the CURL function at the Controller layer that needs to be called;
<?php namespace Home\Controller;use Think\Controller;class WxJsAPIController extends Controller { public function index(){ //获取接口数据 $url = '此处填写接口地址'; $data_son = curl($url);//调取function.php封装的CURL函数 $data = json_decode($data_json,true)['access_token']; var_dump($data);//JSON数据转换成数组形式 //如果还需要调用CURL函数,同上直接curl("接口地址")即可; } } ?>
The above is all the content of this article, if you If you don’t know much about it yet, you can easily master it if you can implement both sides yourself!
Related recommendations:
thinkphp3.2 uses ajax to delete columns to share examples
thinkPHP3.2 Examples of join usage
The above is the detailed content of Detailed explanation of encapsulating public functions under the TP framework. For more information, please follow other related articles on the PHP Chinese website!