thinkphp不读取.env文件的键对值

第一:$_ENV会为空,其原因通常是php的配置文件php.ini的配置项为:

;variables_order
;Default Value: “EGPCS”
;Development Value: “GPCS”
;Production Value: “GPCS”


要想让$_ENV的值不为空:

;variables_order
Default Value: “EGPCS”
;Development Value: “GPCS”
;Production Value: “GPCS”

第二:

Env不显示的问题,需要在base.php中 putenv的同时,将数据也写入$_ENV,就可以解决了

打开thinkphp目录下的base.php文件修改。

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace think;

use \think\Env;
// ThinkPHP 引导文件
// 1. 加载基础文件
require __DIR__ . '/base.php';

if (is_file(ROOT_PATH . '.env')) {
    $env = parse_ini_file(ROOT_PATH . '.env', true);

    foreach ($env as $key => $val) {
        $name = ENV_PREFIX . strtoupper($key);

        if (is_array($val)) {
            foreach ($val as $k => $v) {
                $item = $name . '_' . strtoupper($k);
                putenv("$item=$v");
            }
        } else {
            putenv("$name=$val");
            //加入这一句
            $_ENV[$name]=$val;
        }
    }
}

// 2. 执行应用
App::run()->send();

.env在application同级目录,文件格式为

status='dev'
[database]

hostname = localhost

database = vxianfeng

username = root

password = root

hostport = 3306

prefix = vxf_

 

posted @ 2019-07-18 17:04  江期玉  阅读(1886)  评论(0编辑  收藏  举报