Home >Backend Development >PHP Tutorial >Detailed explanation of encapsulating public functions under the TP framework

Detailed explanation of encapsulating public functions under the TP framework

韦小宝
韦小宝Original
2018-03-14 17:19:223676browse

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)

  1. Root directory\Application\Common\Common\function.php (Public functions can be called by Home and Admin)

  2. Root directory\Application\Home\Common\function.php (functions common to Home layer, can only be called by Home layer)

  3. 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;

638ad7bfb6980293c6dec1b0010e411a

Step 2: Call the CURL function at the Controller layer that needs to be called;

1a82aa7c19558942beea0fa37da7019c

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn