thinkphp6-配置设置与获取


环境变量

设置环境变量 /.env

[DATABASE]
USERNAME =  root
PASSWORD =  123456

获取环境变量 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);

    }

}

访问测试

http://127.0.0.1:8000/index
{"username":"root","password":"123456"}

配置

配置设置 config/app.php

<?php
// +----------------------------------------------------------------------
// | 应用设置
// +----------------------------------------------------------------------

return [
    // 应用地址
    'app_host'         => env('app.host', ''),
    // 应用的命名空间
    'app_namespace'    => '',
    // 是否启用路由
    'with_route'       => true,
    // 默认应用
    'default_app'      => 'index',
    // 默认时区
    'default_timezone' => 'Asia/Shanghai',

    // 应用映射(自动多应用模式有效)
    'app_map'          => [],
    // 域名绑定(自动多应用模式有效)
    'domain_bind'      => [],
    // 禁止URL访问的应用列表(自动多应用模式有效)
    'deny_app_list'    => [],

    // 异常页面的模板文件
    'exception_tmpl'   => app()->getThinkPath() . 'tpl/think_exception.tpl',

    // 错误显示信息,非调试模式有效
    'error_message'    => '页面错误!请稍后再试~',
    // 显示错误信息
    '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);

    }

}

访问测试与结果

http://127.0.0.1:8000/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"
}

新配置文件

添加配置文件 /config/test.php

<?php

return [
        'name'=>'huyongjian'
];

获取配置

<?php
namespace app\controller;

use app\BaseController;
use think\facade\Config;

class Index extends BaseController
{
    public function index()
    {
        $test = Config::get('test');
        $name = Config::get('test.name');
        $data = [
                'test' => $test,
                'name' => $name,
        ];

        return json_encode($data);

    }

}

访问测试与结果

http://127.0.0.1:8000/index
{"test":{"name":"huyongjian"},"name":"huyongjian"}

系统配置文件

配置文件名       描述
app.php         应用配置
cache.php       缓存配置
console.php     控制台配置
cookie.php      Cookie配置
database.php    数据库配置
filesystem.php  磁盘配置
lang.php        多语言配置
log.php         日志配置
middleware.php    中间件配置
route.php       路由和URL配置
session.php     Session配置
trace.php       页面Trace配置
view.php        视图配置


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM