thinkphp 5.0 Configuration

Source: Internet
Author: User

thinkphp 5.0 Configuration Directory
<!--系统默认的配置文件目录就是应用目录(APP_PATH),也就是默认的application下面,并分为应用配置(整个应用有效)和模块配置(仅针对该模块有效)。-->├─application         应用目录│  ├─config.php       应用配置文件│  ├─database.php     数据库配置文件│  ├─route.php        路由配置文件│  ├─index            index模块配置文件目录│  │  ├─config.php    index模块配置文件│  │  └─database.php  index模块数据库配置文件<!--如果不希望配置文件放到应用目录下面,可以在入口文件中定义独立的配置目录,添加CONF_PATH常量定义即可,如下:-->// 定义配置文件目录和应用目录同级define(‘CONF_PATH‘, __DIR__.‘/../config/‘);// 扩展配置目录<!--5.0.1开始增加了扩展配置目录的概念,在应用配置目录或者模块配置目录下面增加extra子目录,下面的配置文件都会自动加载,无需任何配置。--><!--如果你定义了CONF_PATH常量为config目录为例,扩展配置目录如下:-->├─application         应用目录├─config              配置目录│  ├─config.php       应用配置文件│  ├─database.php     数据库配置文件│  ├─route.php        路由配置文件│  ├─extra            应用扩展配置目录│  ├─index            index模块配置文件目录│  │  ├─extra         index模块扩展配置目录│  │  ├─config.php    index模块配置文件│  │  └─database.php  index模块数据库配置文件<!--扩展配置文件的文件名(不含后缀)就是配置参数名,并且会和应用配置文件中的参数进行合并。-->
Configuration format

The configuration parameter names are case-insensitive (because the case definition is converted to lowercase), the new recommendation is to use lowercase to define specifications for configuration parameters.

// 数组 项目配置文件return [    // 默认模块名    ‘default_module‘        => ‘index‘,    // 默认控制器名    ‘default_controller‘    => ‘Index‘,    // 默认操作名    ‘default_action‘        => ‘index‘,    //更多配置参数    //...];// 二维数组 项目配置文件return [    ‘cache‘                 => [        ‘type‘   => ‘File‘,        ‘path‘   => CACHE_PATH,        ‘prefix‘ => ‘‘,        ‘expire‘ => 0,    ],];

Additional Configuration format support

By default, the configuration file is defined as a PHP array, and you can define the Conf_ext constant in the portal file to change to a different configuration type:

Change the configuration format to INI format
Define (' Conf_ext ', '. ini ');

<!--ini格式配置示例:-->default_module=Index ;默认模块default_controller=index ;默认控制器default_action=index ;默认操作<!--xml格式配置示例:--><config><default_module>Index</default_module><default_controller>index</default_controller><default_action>index</default_action></config><!--json格式配置示例:-->{"default_module":"Index","default_controller":"index","default_action":"index"}

Second-level configuration

Configuration parameters support Level Two, for example, the following is an example of a level two configuration setting and reading:

$config = [    ‘user‘  =>  [        ‘type‘  =>  1,        ‘name‘  =>  ‘thinkphp‘,    ],    ‘db‘    =>  [        ‘type‘      =>  ‘mysql‘,        ‘user‘      =>  ‘root‘,        ‘password‘  =>  ‘‘,    ],];// 设置配置参数Config::set($config);// 读取二级配置参数echo Config::get(‘user.type‘);// 或者使用助手函数echo config(‘user.type‘);

The system does not support the reading of configuration parameters above the second level and requires manual step reading.
Operation with a two-level configuration is still supported in the case of a scope.

Configuration load
    1. Load order: In thinkphp, the configuration files that are generally applied are loaded automatically, and the order of loading is:
惯例配置-》应用配置-》扩展配置-》场景配置-》模块配置-》动态配置
    1. Formula configuration: The formula is more important than the configuration is a systematic follow-up idea, the framework has a custom configuration file (located in thinkphp/convention.php), according to most of the use of common parameters are configured by default. Therefore, for the application configuration file, often only need to configure and custom configuration of different or new configuration parameters, if you fully adopt the default configuration, you can even do not need to define any configuration file
    2. Application configuration: The application configuration file is the public profile that is loaded first when the app is initialized, and is located by default in application/config.php.
    3. Extended configuration: The extension profile is an additional configuration file defined by the Extra_config_list configuration parameter, and the database and validate two extension profiles are loaded by default.
    4. Scenario configuration: Different business scenarios use the specified configuration file, and the scene profile and app profile config.php are the same definition.
    5. Module configuration: Each module will automatically load its own configuration file (located at application/current module name/config.php). Modules can also support stand-alone status profiles, named as: application/Current module name/application state. php.
    6. Dynamic configuration:
Config::set(‘配置参数‘,‘配置值‘);// 或者使用助手函数config(‘配置参数‘,‘配置值‘);// 批量设置Config::set([    ‘配置参数1‘=>‘配置值‘,    ‘配置参数2‘=>‘配置值‘]);// 或者使用助手函数config([    ‘配置参数1‘=>‘配置值‘,    ‘配置参数2‘=>‘配置值‘]);
Read configuration
    1. echo config::get (' configuration parameter 1 ');
    2. echo config (' configuration parameter 1 ');
    3. Dump (Config::get ()); | | Dump (config ());
    4. Config::has (' configuration parameter 2 '); | | Config ('? config parameter 2 ');
    5. echo config::get (' configuration parameters. Two-level parameters ');
      echo config (' configuration parameters. Two-level parameters ');
Standalone configuration file

Configuration Files support separation (also known as extended configuration) by simply configuring the Extra_config_list parameter in the public profile (the V5.0.1 version has already been repealed).

For example, if you do not use a standalone configuration file, the database configuration information should be configured in config.php as follows:/* Database Settings */"database" + [//DB-type ' type ' = ' + ' m Ysql ',//server address ' hostname ' = ' 127.0.0.1 ',//Database name ' db ' = ' thinkphp ',//database username ' user Name ' + ' root ',//Database password ' password ' + ',//Database connection port ' Hostport ' + ',//database connection parameter ' P Arams ' = + [],//Database encoding defaults to UTF8 ' charset ' = ' utf8 ',//database table prefix ' prefix ' + ',//Database Debug mode ' Debug ' = False,] If you need to use a standalone configuration file, first add the configuration in config.php: ' extra_config_list ' = [' database '], after definition,    Configuration can be used standalone database.php file, configuration content as follows:/* Database Settings */return [//Database type ' type ' = ' mysql ',//server address ' hostname '     = ' 127.0.0.1 ',//database name ' thinkphp ',//database user name ' username ' + ' root ',//Database password ' Password ' = ' + ',//Database connection port ' Hostport ' + ',//database connection parameter ' params ' = [],//Database encoding default    Use UTF8 ' charset ' = = ' UTF8 ',//database table prefix ' prefix ' + ',//Database debug mode ' Debug ' = False,] If the extra_config_list is configured Parameters are configured in both the config.php and database.php files, the configuration of the database.php file overrides the settings in the config.php. The parameter acquisition of the standalone configuration file is a two-dimensional configuration, for example, to obtain the type parameter of the database standalone configuration file, it should be: config::get (' Database.type '); to get the parameters for a complete standalone profile, use: Config:: Get (' database ');
Environment variables
环境变量配置,在此框架中.env的用法与laravel的环境变量配置相同ThinkPHP5.0支持使用环境变量配置。在开发过程中,可以在应用根目录下面的.env来模拟环境变量配置,.env文件中的配置参数定义格式采用ini方式,例如:app_debug =  trueapp_trace =  true如果你的部署环境单独配置了环境变量,那么请删除.env配置文件,避免冲突。环境变量配置的参数会全部转换为大写,值为 null,no 和 false 等效于 "",值为 yes 和 true 等效于 "1"。ThinkPHP5.0默认的环境变量前缀是PHP_,也可以通过改变ENV_PREFIX常量来重新设置。注意,环境变量不支持数组参数,如果需要使用数组参数可以,使用下划线分割定义配置参数名:database_username =  rootdatabase_password =  123456或者使用[database]username =  rootpassword =  123456获取环境变量的值可以使用下面的两种方式获取:Env::get(‘database.username‘);Env::get(‘database.password‘);// 同时下面的方式也可以获取Env::get(‘database_username‘);Env::get(‘database_password‘);可以支持默认值,例如:// 获取环境变量 如果不存在则使用默认值rootEnv::get(‘database.username‘,‘root‘);可以直接在应用配置中使用环境变量,例如:return [    ‘hostname‘  =>  Env::get(‘hostname‘,‘127.0.0.1‘),];环境变量中设置的app_debug和app_trace参数会自动生效(优先于应用的配置文件),其它参数则必须通过Env::get方法才能读取。

thinkphp 5.0 Configuration

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.