thinkphp5.1 does not support extra. The config function in thinkphp5.1 has canceled the support for extra; you can change "function editConfig($arr = [],$user='admin'){...} ” Paste the code into common.php in the app or application folder to use the function globally.
The operating environment of this tutorial: Windows 7 system, ThinkPHP version 5, Dell G3 computer.
Does thinkphp5.1 support extra?
not support.
ThinkPHP5.1 Use files as configuration files
ThinkPHP5.1 Use files as configuration files for pitfall records
Use Tp5.1 (version limited) as the App backend At that time, users made frequent requests, and every step of the operation required access to the database. Every step of the operation required access to the config configuration table. Frequent reading put a lot of pressure on the database. Monitoring the traffic in the background, it was found that nearly 30% of the requests were accessing config table, so I decided to transfer the configuration to the local phone and use the file as the configuration.
However, after querying various information, I could not find the configuration that can be modified statically. The Config::set() function provided by Tp5.1 can only The configuration file is dynamically modified, and it is limited to this controller. The actual configuration file has not changed, which obviously does not meet our requirements.
After checking various information, I found this article to be the most reliable, but in actual testing Invalid,
After entering the config function, I found that in Tp5.1, the config function had canceled the extra support, so I rewrote it myself
Without further ado, I just violently added the code.
/** * 修改扩展配置文件 * @param array $arr 需要更新或添加的配置 * @param string $user 修改人 * @return bool */ function editConfig($arr = [] ,$user='admin') { if (is_array($arr)) { //获取文件名 $filename = 'business.php'; //获取配置文件环境变量位置(请确保开启权限,如若报错,请改为绝对路径) $filepath = Env::get('CONFIG_PATH'). $filename; //判定配置文件是否存在 if (!file_exists($filepath) ) { if(!fopen($filepath, "w")){ return 'PermissionError1'; } } //判定权限是否足够 if (!is_writable($filepath)) { return 'PermissionError2'; } //遍历整个配置文件 $conf = include $filepath; foreach ($arr as $key => $value) { $conf[$key] = $value; } //记录修改者 $time = date('Y/m/d H:i:s'); $str = "<?php\r\n/**\r\n * 由".$user."修改.\r\n * $time\r\n */\r\nreturn [\r\n"; //写入配置文件 foreach ($conf as $key => $value) { if(is_array($value)){ $str.="\t'$key'=>[\r\n"; foreach ($value as $ikey=>$r) { if(is_numeric($ikey)){ $str .= "\t\t'$r',"; $str .= "\r\n"; }else{ $str .= "\t\t'$ikey' => '$r',"; $str .= "\r\n"; } } $str = rtrim($str,','); $str .= "\t],"."\r\n"; } else{ $str .= "\t'$key' => '$value',"; $str .= "\r\n"; } } $str .= '];'; //关闭文件 $result = file_put_contents($filepath, $str); if($result){ return 'success'; } else { return $result; } } else { return 'error'; } }
Paste this code into common.php in the app (or application) folder, and you can use this function globally to modify the configuration file.
Example:
Create a business.php file in the config folder in the root directory of the website (note the read and write permissions),
Call the modification function in the controller
public function setBusiness(){ $arr = array( "WEB" => [ "web_status"=>'1', 1,3,4 ], ); $result=editConfig($arr,'admin123'); if($result=='success'){ echo ('修改成功'); }elseif($result=='error'){ echo ('修改失败'); } elseif($result=='PermissionError'){ echo ('文件无权限,请联系管理员'); } }
Configuration under the config folder The file will be modified to
<?php /** * 由admin123修改. * 2019/11/22 13:00:27 */ return [ 'WEB'=>[ 'web_status' => '1', '1', '3', '4', ], ];
Next, in other controllers, you can directly use the config rules to obtain various configuration files. For specific rules, please move to the TP5.1 manual configuration acquisition chapter.
The logic program reads the configuration directly from the file. When the user reads the configuration, you can set up a cache or read it directly from redis.
Recommended learning: "thinkPHP Video Tutorial 》
The above is the detailed content of Does thinkphp5.1 support extra?. For more information, please follow other related articles on the PHP Chinese website!