Home > PHP Framework > ThinkPHP > Does thinkphp5.1 support extra?

Does thinkphp5.1 support extra?

藏色散人
Release: 2022-12-12 09:38:30
Original
1916 people have browsed it

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.

Does thinkphp5.1 support extra?

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&#39;$key&#39;=>[\r\n";
                foreach ($value as $ikey=>$r) {
                    if(is_numeric($ikey)){
                        $str .= "\t\t&#39;$r&#39;,";
                        $str .= "\r\n";
                    }else{
                        $str .= "\t\t&#39;$ikey&#39; => &#39;$r&#39;,";
                        $str .= "\r\n";
                    }
                }
                $str = rtrim($str,&#39;,&#39;);
                $str .= "\t],"."\r\n";
            } else{
                $str .= "\t&#39;$key&#39; => &#39;$value&#39;,";
                $str .= "\r\n";
            }
        }
        $str .= &#39;];&#39;;
        //关闭文件
        $result = file_put_contents($filepath, $str);
        if($result){
            return &#39;success&#39;;
        } else {
            return $result;
        }
    } else {
        return &#39;error&#39;;
    }
}
Copy after login

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"=>&#39;1&#39;,
                1,3,4
            ],
        );
        $result=editConfig($arr,&#39;admin123&#39;);
        if($result==&#39;success&#39;){
            echo (&#39;修改成功&#39;);
        }elseif($result==&#39;error&#39;){
            echo (&#39;修改失败&#39;);
        }
        elseif($result==&#39;PermissionError&#39;){
            echo (&#39;文件无权限,请联系管理员&#39;);
        }
    }
Copy after login

Configuration under the config folder The file will be modified to

<?php
/**
 * 由admin123修改.
 * 2019/11/22 13:00:27
 */
return [
&#39;WEB&#39;=>[
&#39;web_status&#39; => &#39;1&#39;,
&#39;1&#39;,
&#39;3&#39;,
&#39;4&#39;,
],
];
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template