How to rewrite the configuration file in ThinkPHP

Source: Internet
Author: User
Tags php file server port


In some mature CMS systems, the background usually contains a configuration center (such as the system settings in the background of the dream) to facilitate the webmaster to modify the configuration file in the background; what if this function is implemented? Is there a shortcut in ThinkPHP? The answer is yes. The following describes how to implement this function:

Read the original configuration items to the form to complete the form presentation; so that the administrator can modify the configuration
After the administrator changes the configuration form value, click Submit. The program receives the corresponding values for permanent storage (such as database storage and file storage)
Next, follow these steps to read and rewrite the database configuration file. When you complete the following code, create a new db_config.php file in the project configuration file directory and write the database configuration items as follows ):


<? Php
/*
* Database configuration file
*/
 
Return array (
// Database configuration
'Db _ host' => '2017. 0.0.1 ',
'Db _ port' => '123 ',
'Db _ user' => 'root ',
'Db _ pwd' => '',
'Db _ name' => 'dbname ',
'Db _ prefix' => 'ly _'
);
?>
Add 'load _ EXT_CONFIG '=> 'DB _ config' to the main configuration file config. php of the project to LOAD the current database configuration file.

The following is the page view configured by the administrator:


<Form action = "{: U (GROUP_NAME. '/DbConfig/updateDbconfig')}" method = "post">
<Table>
<Tr>
<Th rowspan = "2"> database configuration </th>
</Tr>
<Tr>
<Td align = "right"> database server address: </td>
<Td> <input type = "text" name = "DB_HOST" value = "{$ Think. config. DB_HOST}"/> </td>
</Tr>
<Tr>
<Td align = "right"> database server port: </td>
<Td> <input type = "text" name = "DB_PORT" value = "{$ Think. config. DB_PORT}"/> </td>
</Tr>
<Tr>
<Td align = "right"> database username: </td>
<Td> <input type = "text" name = "DB_USER" value = "{$ Think. config. DB_USER}"/> </td>
</Tr>
<Tr>
<Td align = "right"> database password: </td>
<Td> <input type = "text" name = "DB_PWD" value = "{$ Think. config. DB_PWD}"/> </td>
</Tr>
<Tr>
<Td align = "right"> database name: </td>
<Td> <input type = "text" name = "DB_NAME" value = "{$ Think. config. DB_NAME}"/> </td>
</Tr>
<Tr>
<Td align = "right"> database table prefix: </td>
<Td> <input type = "text" name = "DB_PREFIX" value = "{$ Think. config. DB_PREFIX}"/> </td>
</Tr>
<Tr>
<Td align = "center" colspan = "2"> <input type = "submit" value = "save"/> </td>
</Tr>
</Table>
</Form>
The corresponding DbConfigAction. class. php controller code is as follows:


<? Php
/**
* Created by PhpStorm.
* User: LiuYang
* Date: 14-10-11
* Time: PM
* Description: database configuration controller
*/
 
Class DbConfigAction extends Action {
   
// Modify the configuration view
Public function dbConfig (){
$ This-> display ();
    }
 
// Rewrite the configuration file form for processing
Public function updateDbconfig (){
 
If (! IS_POST) halt ("the page you accessed does not exist. Please try again later! ");
 
If (F ('Db _ config', $ _ POST, CONF_PATH )){
$ This-> success ('modified successfully', U (GROUP_NAME. '/DbConfig/dbconfig '));
} Else {
$ This-> error ('modification failed, please modify '. CONF_PATH. 'DB _ config. Php file authorization ');
        }
    }
}
To rewrite the configuration file, we will briefly describe the following content:

{$ Think. config. DB_HOST}: obtain the original value of the corresponding item in ThinkPhp. Of course, you can also write {: C ('Db _ host')} like this ')}.
F ('Db _ config', $ _ POST, CONF_PATH): F function (functions in the Common directory of ThinkPHP. php file) is a function used to write cached files in Thinkphp. This function also applies. The input parameters are file name (default suffix. php), written data, and file location. The above CONF_PATH is a constant in ThinkPHP, indicating the location of the main configuration file.

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.