環(huán)境變量
設(shè)置環(huán)境變量 /.env
[DATABASE]
USERNAME = root
PASSWORD = 123456
獲取環(huán)境變量 app/controller/Index.php
<?php
namespace app\controller;
use app\BaseController;
use think\facade\Env;
class Index extends BaseController
{
public function index()
{
$username = Env::get('database.username','root');
$password = Env::get('database.password');
$data = [
'username' => $username,
'password' => $password
];
return json_encode($data);
}
}
訪問測(cè)試
http://codeit.org.cn/index
{“username”:“root”,“password”:“123456”}
1.在thinkphp\config\下新建一個(gè)test.php配置文件
.test.php文件內(nèi)容
<?php
// 自定義配置文件
return [
'profile' => [
'name' => 'vcncn.cn',
'bank' => [
'ABC' => '123',
'vcncn']
]
];
調(diào)用test.php配置文件
// 1.調(diào)用整個(gè)數(shù)組
$testConfig = \think\facade\Config::get('test');
//注意//test.php 就是配置文件名稱
// 2.只調(diào)用鍵名 profile 下的數(shù)組
$profile = \think\facade\Config::get('test.profile');
// 3.調(diào)用索引鍵的數(shù)組
$val = \think\facade\Config::get('test.profile.bank.0');
配置
配置設(shè)置 config/app.php
<?php
// +----------------------------------------------------------------------
// | 應(yīng)用設(shè)置
// +----------------------------------------------------------------------
return [
// 應(yīng)用地址
'app_host' => env('app.host', ''),
// 應(yīng)用的命名空間
'app_namespace' => '',
// 是否啟用路由
'with_route' => true,
// 默認(rèn)應(yīng)用
'default_app' => 'index',
// 默認(rèn)時(shí)區(qū)
'default_timezone' => 'Asia/Shanghai',
// 應(yīng)用映射(自動(dòng)多應(yīng)用模式有效)
'app_map' => [],
// 域名綁定(自動(dòng)多應(yīng)用模式有效)
'domain_bind' => [],
// 禁止URL訪問的應(yīng)用列表(自動(dòng)多應(yīng)用模式有效)
'deny_app_list' => [],
// 異常頁(yè)面的模板文件
'exception_tmpl' => app()->getThinkPath() . 'tpl/think_exception.tpl',
// 錯(cuò)誤顯示信息,非調(diào)試模式有效
'error_message' => '頁(yè)面錯(cuò)誤!請(qǐng)稍后再試~',
// 顯示錯(cuò)誤信息
'show_error_msg' => false,
];
配置獲取
<?php
namespace app\controller;
use app\BaseController;
use think\facade\Config;
class Index extends BaseController
{
public function index()
{
$app = Config::get('app');
$timezone = Config::get('app.default_timezone');
$data = [
'app' => $app,
'timezone' => $timezone,
];
return json_encode($data);
}
}
訪問測(cè)試與結(jié)果
http://vcncn.cn/index
{
“app”: {
“app_host”: “”,
“app_namespace”: “”,
“with_route”: true,
“default_app”: “index”,
“default_timezone”: “Asia/Shanghai”,
“app_map”: [],
“domain_bind”: [],
“deny_app_list”: [],
“exception_tmpl”: “/private/var/www/tp/vendor/topthink/framework/src/tpl/think_exception.tpl”,
“error_message”: “\u9875\u9762\u9519\u8bef\uff01\u8bf7\u7a0d\u540e\u518d\u8bd5\uff5e”,
“show_error_msg”: false
},
“timezone”: “Asia/Shanghai”
}文章來源:http://www.zghlxwxcb.cn/news/detail-549767.html
新配置文件
添加配置文件 /config/test.php文章來源地址http://www.zghlxwxcb.cn/news/detail-549767.html
到了這里,關(guān)于thinkphp6-配置設(shè)置與獲取,Thinkphp6自定義配置文件以及調(diào)用(config文件夾下的配置)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!