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!

This article compares Lenovo's ThinkBook and ThinkPad laptop lines. ThinkPads prioritize durability and performance for professionals, while ThinkBooks offer a stylish, affordable option for everyday use. The key differences lie in build quality, p

This article explains how to prevent SQL injection in ThinkPHP applications. It emphasizes using parameterized queries via ThinkPHP's query builder, avoiding direct SQL concatenation, and implementing robust input validation & sanitization. Ad

This tutorial addresses common ThinkPHP vulnerabilities. It emphasizes regular updates, security scanners (RIPS, SonarQube, Snyk), manual code review, and penetration testing for identification and remediation. Preventative measures include secure

This article addresses ThinkPHP vulnerabilities, emphasizing patching, prevention, and monitoring. It details handling specific vulnerabilities via updates, security patches, and code remediation. Proactive measures like secure configuration, input

This article details ThinkPHP software installation, covering steps like downloading, extraction, database configuration, and permission verification. It addresses system requirements (PHP version, web server, database, extensions), common installat

This article demonstrates building command-line applications (CLIs) using ThinkPHP's CLI capabilities. It emphasizes best practices like modular design, dependency injection, and robust error handling, while highlighting common pitfalls such as insu

This guide details database connection in ThinkPHP, focusing on configuration via database.php. It uses PDO and allows for ORM or direct SQL interaction. The guide covers troubleshooting common connection errors, managing multiple connections, en

This article introduces ThinkPHP, a free, open-source PHP framework. It details ThinkPHP's MVC architecture, features (routing, database interaction), advantages (rapid development, ease of use), and disadvantages (potential over-engineering, commun


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
