search
HomePHP FrameworkThinkPHPDoes thinkphp5.1 support extra?
Does thinkphp5.1 support extra?Dec 12, 2022 am 09:38 AM
thinkphp5.1extra

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;;
    }
}

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;);
        }
    }

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;,
],
];

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!

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
What is the difference between think book and thinkpadWhat is the difference between think book and thinkpadMar 06, 2025 pm 02:16 PM

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

How to prevent SQL injection tutorialHow to prevent SQL injection tutorialMar 06, 2025 pm 02:10 PM

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

How to fix thinkphp vulnerability How to deal with thinkphp vulnerabilityHow to fix thinkphp vulnerability How to deal with thinkphp vulnerabilityMar 06, 2025 pm 02:04 PM

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

How to deal with thinkphp vulnerability? How to deal with thinkphp vulnerabilityHow to deal with thinkphp vulnerability? How to deal with thinkphp vulnerabilityMar 06, 2025 pm 02:08 PM

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

How to install the software developed by thinkphp How to install the tutorialHow to install the software developed by thinkphp How to install the tutorialMar 06, 2025 pm 02:09 PM

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

How can I use ThinkPHP to build command-line applications?How can I use ThinkPHP to build command-line applications?Mar 12, 2025 pm 05:48 PM

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

Detailed steps for how to connect to the database by thinkphpDetailed steps for how to connect to the database by thinkphpMar 06, 2025 pm 02:06 PM

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

How to use thinkphp tutorialHow to use thinkphp tutorialMar 06, 2025 pm 02:11 PM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft