About the use of ThinkPHP3.2 framework custom configuration and loading

Source: Internet
Author: User
This article mainly introduces the ThinkPHP3.2 framework custom configuration and loading usage, and analyzes the thinkPHP3.2 framework using the custom configuration to save the predefined array data related operation skills with the example form, the friend who needs can refer to the following

This example describes the ThinkPHP3.2 framework custom configuration and load usage. Share to everyone for your reference, as follows:

Sometimes we have rules that define the actual content of each number, such as the drop-down menu:

<select name= "Reasonandtype" id= "" >  <option value= "1" > Query logistics </option>  <option value= "2" > Return </option>  <option value= "3" > Inquiry return </option>  <option value= "4" > Consulting Commission </ option>  <option value= "5" > Advisory Rules </option>  <option value= "0" > Other </option></ Select>

← Actual effect

← The effect of database storage

← When it is displayed.

In this example, the actual display is the Chinese character, the actual value is a number, and so we save to the database when it is also stored numbers, and so we get from the database when the number is taken, the display is also displayed Chinese characters;

So we need a dictionary-like thing that defines what the corresponding Chinese character is, and what the actual meaning of the definition is;

Using the database can achieve this effect, but we do not, because the operation of the database is too expensive;

If it is a normal PHP environment, we can save the rules to a single file, the content is an array:

Type. Php

$type = Array (' 1 ' = ' query logistics ', ' 2 ' = ' return ' for Returns ', ' 3 ' = ' consulting returns ', ' 4 ' = ' Consulting Commission ', ' 5 ' + ' advisory Rules ', ' 0 ' = ' other ');

As the code above, after the definition, save as a file, when we need to call this rule, the include or require, and then directly with the $type variable to find it, such as:

Require_once (' type.php '); echo $type [1]; Output ' Query Logistics ' foreach ($type as $k + $v) {//print out the top drop-down menu code directly, echo "<option value = ' {$k} ' >{$v}</option>";}

If in thinkphp, if want to introduce a file, can only use absolute path, in include and require use __APP__ or __ROOT__ fast entrance is not, U() chain function I did not try, should be achievable.

There is also a way of implementation, and I recommend here, is to directly define this set of rules as the site's configuration file, when we use the C() load configuration function to load the array to assign to a variable.

On the code:

Put your defined rules under Conf, this is a private profile, only the home entry to take effect, you can also put in the public profile, the details can be found in the manual.

Return Array (' reason_and_type ' = + Array (' 1 ' = ' query logistics ', ' 2 ' = ' = ' for return ', ' 3 ' = ' consulting returns ', ' 4 ' = ' Consulting Commission ', ' 5 ' = ') ' Advisory rules ', ' 0 ' = ' other ');

Note plus return;

Note that the format, return an array, the key name is uppercase (in order to look good), the key value is what you want to define, here we define an array.

Modify the config.php file

<?phpreturn Array (//' configuration item ' = = ' config value ' db_type ' = ' mysqli ',//database type ' db_host ' = ' localhost ',//server address ' db_name ' = > ' Kefu ',//database name ' Db_user ' + ' root ',//user name ' Db_pwd ' + ' root ',//password ' db_port ' + ', '//Port ' db_prefix ' = ' ',//database table prefix ' load_ext_config ' = ' reasonandtype ',//This is our own addition, Load_ext_config key name is fixed, the key value is your PHP file name, the array name is not in the tube here. ;

This is even a good configuration.

Here's how to call this set of rules:

$reasonAndTypeX = C (' Reason_and_type ');//directly with the array name, the C () function can load the value represented by the key name in the configuration, assign to the variable

Note that the C() value inside the function is reasonandtype the value of this file.

Var_dump ($reasonAndTypeX);

The contents of this array will be entered.

Array (' 1 ' = ' query logistics ', ' 2 ' = ' return ' for Returns ', ' 3 ' = ' "consulting returns ', ' 4 ' = ' Consulting Commission ', ' 5 ' + ' advisory Rules ', ' 0 ' = ' other ');

This completes the setup and loading of the custom configuration file.

If 'reason_and_type' is not configured, a null value is returned;

Of course, you can also set the default value, the code is as follows:

Returns the Default_config string C (' My_config ', null, ' Default_config ') if Reason_and_type is not already set;

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related Article

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.