Merge ThinkPHP configuration files to eliminate code redundancy. _ PHP Tutorial

Source: Internet
Author: User
Merge the ThinkPHP configuration file to eliminate code redundancy ,. Merge the ThinkPHP configuration file to eliminate code redundancy. many times, when we use ThinkPHP to configure the database connection between the front-end and back-end of the website, the ThinkPHP configuration files are often merged to eliminate code redundancy,

Many times, when we use ThinkPHP to configure the database connection between the front-end and the back-end of the website, we usually write the configuration separately in the front-end configuration file and the back-end configuration file. 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.




Frequently, when we use ThinkPHP to configure the database connection between the website front-end and the website back-end, they are always in front of each other...

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.