This article mainly introduces how to merge ThinkPHP configuration files to eliminate code redundancy. it is a very practical technique, for more information about ThinkPHP, the configuration files in the front-end and the backend are written separately. However, many times the configuration of the front-end and back-end databases may be the same, but the two files use the same configuration, so the code generates redundancy.
The front-end of the website has a good database, such as user registration, user login, and comments. we need to use the database on the front-end. now that the database is used, we have to connect to the database! There is no need to talk about the website background, and databases are everywhere.
Most websites use a database at the front and back ends, that is, the configuration information for connecting databases at the front and back ends is the same. However, the problem arises. If ThinkPHP is used, some friends may be the configuration files in the frontend and backend, that is, the config. php files in the Conf folder all write the configuration information for connecting to the database. At this time, code redundancy is inevitable.
In this case, if the server configuration is changed, both configuration files need to be rewritten. a slight omission may cause significant losses. To eliminate redundancy, it is necessary to merge configuration files. The specific measures are as follows:
Create a php file in the same directory as the front-end and back-end of the website. for example, name it config. inc. php and write the database configuration information in this file. Example:
<?phpreturn array('DB_TYPE=>'mysql','DB_NAME'=>'demo','DB_HOST'=>'localhost','DB_USER'=>'root','DB_PWD'=>'123456','DB_PREFIX'=>'demo_');?>
Well, write the following in the front-end and backend configuration files:
<? Php $ arr01 = array (// Other foreground or background configuration information); $ arr02 = include '. /config. inc. php '; // combine the two arrays return array_merge ($ arr01, $ arr02);?>
This method can effectively solve code redundancy issues. When changing the database address, you only need to modify the config. inc. php configuration file.