配置格式
ThinkPHP支持多種格式的配置格式,但最終都是解析為PHP數(shù)組的方式。
PHP數(shù)組定義
返回PHP數(shù)組的方式是默認(rèn)的配置定義格式,例如:
//項(xiàng)目配置文件
return [
// 默認(rèn)模塊名
'default_module' => 'index',
// 默認(rèn)控制器名
'default_controller' => 'Index',
// 默認(rèn)操作名
'default_action' => 'index',
//更多配置參數(shù)
//...
];
配置參數(shù)名不區(qū)分大小寫(因?yàn)闊o論大小寫定義都會(huì)轉(zhuǎn)換成小寫),新版的建議是使用小寫定義配置參數(shù)的規(guī)范。
還可以在配置文件中可以使用二維數(shù)組來配置更多的信息,例如:
//項(xiàng)目配置文件
return [
'cache' => [
'type' => 'File',
'path' => CACHE_PATH,
'prefix' => '',
'expire' => 0,
],
];
其他配置格式支持
默認(rèn)方式為PHP數(shù)組方式定義配置文件,你可以在入口文件定義CONF_EXT
常量來更改為其它的配置類型:
// 更改配置格式為ini格式
define('CONF_EXT', '.ini');
配置后,會(huì)自動(dòng)解析支持的配置類型,包括.ini
、.xml
、.json
和 .php
在內(nèi)的格式支持。
5.0版本開始支持yaml配置格式支持
ini格式配置示例:
default_module=Index ;默認(rèn)模塊
default_controller=index ;默認(rèn)控制器
default_action=index ;默認(rèn)操作
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"
}
二級(jí)配置
配置參數(shù)支持二級(jí),例如,下面是一個(gè)二級(jí)配置的設(shè)置和讀取示例:
$config = [
'user' => [
'type' => 1,
'name' => 'ThinkPHP',
],
'db' => [
'type' => 'mysql',
'user' => 'root',
'password' => '',
],
];
// 設(shè)置配置參數(shù)
Config::set($config);
// 讀取二級(jí)配置參數(shù)
echo Config::get('user.type');
// 或者使用助手函數(shù)
echo config('user.type');
系統(tǒng)不支持二級(jí)以上的配置參數(shù)讀取,需要手動(dòng)分步驟讀取。
有作用域的情況下,仍然支持二級(jí)配置的操作。
如果采用其他格式的配置文件的話,二級(jí)配置定義方式如下(以ini和xml為例):
[user]
type=1
name=ThinkPHP
[db]
type=mysql
user=rot
password=''
標(biāo)準(zhǔn)的xml格式文件定義:
<config>
<user>
<type>1</type>
<name>ThinkPHP</name>
</user>
<db>
<type>mysql</type>
<user>root</user>
<password></password>
</db>
</config>
set方法也支持二級(jí)配置,例如:
Config::set([
'type' => 'file',
'prefix' => 'think'
],'cache');
文檔最后更新時(shí)間:2018-04-25 18:00:23
未解決你的問題?請(qǐng)到「問答社區(qū)」反饋你遇到的問題