thinkphp 3.2.3 How to load public function files automatically

Source: Internet
Author: User

Method One, load the default public function file

In thinkphp 3.2.3, the default public function file is located in the public module./application/common, the following configuration file (conf/config.php) and common function files (Common) are loaded before all modules are accessed. /function.php), that is, the default public function file is./application/common/common/function.php.

For example, create a new function.php under./application/common/common, and customize a formatted function

<? PHP // formatted output function p ($var) {    dump ($vartruenull, 0);}

New method in controller./application/home/controller/indexcontroller.class.php:

     Public function test_function () {        p ($_server);    }

Printing results:

Array([Redirect_script_url]=/home/index/test_function [Redirect_script_uri]= = http://127.0.0.100/home/index/test_function[Redirect_status] = 200[Script_url]=/home/index/test_function [Script_uri]= = http://127.0.0.100/home/index/test_function[Http_host] = 127.0.0.100[Http_user_agent]= = mozilla/5.0 (Windows NT 6.1; rv:43.0) gecko/20100101 firefox/43.0[Http_accept]= text/html,application/xhtml+xml,application/xml;q=0.9,*/*; q=0.8 [Http_accept_language] = zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3 [http_accept_encoding] = gzip, deflate [Http_cookie] = thinkphp_show_page_trace=0|1; PHPSESSID=QK6BAHLMIQJ8B52O309DL813B5;  thinkphp_show_page_trace=0|1 [Http_connection] = keep-alive [Http_cache_control] = max-age=0 [PATH] = = C:\windows\system32; C:\windows; C:\windows\System32\Wbem;    C:\windows\System32\WindowsPowerShell\v1.0\;D: \program Files\tortoisesvn\bin;d:\program Files\subversion\bin; [SystemRoot] = C:\windows [COMSPEC] = C:\windows\system32\cmd.exe [Pathext] =. COM;. EXE;. BAT;. CMD;. VBS;. VBE;. JS;. JSE;. WSF;. WSH;.    MSC [windir] = C:\windows [Server_signature] = [Server_software] = apache/2.2.21 (Win32) php/5.3.10 [server_name] = 127.0.0.100 [SERVER_ADDR] = 127.0.0.100 [Server_port] [REMOTE_ADDR] = 127 .0.0.1 [Document_root] = d:/practise/php/tptest [Server_admin] = [email protected] [Script_filename] = d:/practise/php/tptest/index.php [Remote_port] = 56500 [Redirect_url] =/Home/Index/t est_function [Gateway_interface] = cgi/1.1 [Server_protocol] = http/1.1 [Request_method] = = GET [QU  Ery_string] = [Request_uri] =/home/index/test_function [Script_name] =/index.php [path_info] = = index/test_function [path_translated] = redirect:\index.php\home\index\test_function\index\test_function [PHP_ Self] =/index.php/home/index/test_function [Request_time] = 1451725716) 0.0852s[showpagetrace]
View Code

Method Two, load the custom public function file

Sometimes when the number of custom functions in a project is very large, it may be difficult to maintain all of the functions in a file, this time you can choose to split the public function files by function or project, for example, create a new one under the./application/common/common ifunction.php, the file cannot be loaded automatically at this time. There are two ways to load the file automatically:

① add configuration in configuration file./application/common/conf/config.php:

' Load_ext_file ' = ' ifunction ',

The ifunction.php file can be loaded automatically at this point. If there are multiple files that need to be loaded automatically, separate them in the value of the configuration item.

② Temporary loading

Load directly in the controller that needs to use the function file:

     Public function test_function () {        load (' common.ifunction ');   The actual file loaded is./application/common/common/ifunction.php        p ($_server);    }

If the function file is in the same module as the controller that called the function, for example, the function file is./application/home/common/ifunction.php, then./application/home/controller/ IndexController.class.php a method to invoke a function file:

     Public function test_function () {        load (//// The file actually loaded is./application/home/common/ifunction.php,@ represents the current module        p ($_server);    }

thinkphp 3.2.3 How to load public function files automatically

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.