简体   繁体   English

环境变量在Phalcon Config中不起作用

[英]Environment variables not working in Phalcon Config

I have a Phalcon project with this library added: https://github.com/vlucas/phpdotenv . 我添加了这个库的Phalcon项目: https//github.com/vlucas/phpdotenv This library is meant to load some environment variables from a .env file. 该库旨在从.env文件加载一些环境变量。 I created such a file and put it in my project. 我创建了这样一个文件并将其放入我的项目中。

VERSION_NUMBER=3.14 

DATABASE_HOST=localhost
DATABASE_NAME=test
DATABASE_USER=root
DATABASE_PASS=root

I rewrote my loader.php file to the code below: 我将loader.php文件重写为以下代码:

<?php
$loader = new \Phalcon\Loader();

/**
 * We're a registering a set of directories taken from the configuration file
 */
$loader->registerNamespaces(array(
    'Test\Models' => $config->application->modelsDir,
    'Test\Controllers' => $config->application->controllersDir,
    'Test\Forms' => $config->application->formsDir,
    'Test\Classes' => $config->application->classesDir,
    'Test\Classes\Excel' => $config->application->excelDir,
    'Test' => $config->application->libraryDir
));

$loader->register();

// Use composer autoloader to load vendor classes
require_once __DIR__ . '/../../vendor/autoload.php';

$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->required(['DATABASE_HOST', 'DATABASE_NAME', 'DATABASE_USER', 'DATABASE_PASS']);
$dotenv->overload();

And in my config file I replaced the keys with the environment variables: 在我的配置文件中,我用环境变量替换了键:

'database' => array(
    'adapter'  => 'Mysql',
    'host'     => getenv('DATABASE_HOST'),
    'username' => getenv('DATABASE_USER'),
    'password' => getenv('DATABASE_PASS'),
    'dbname'   => getenv('DATABASE_NAME')
),

I can echo getenv('VERSION_NUMBER') where I want and it works every time, but when I try to use this config file the variables are empty. 我可以在我想要的地方回显getenv('VERSION_NUMBER')并且每次都有效,但是当我尝试使用这个配置文件时,变量是空的。 What am I doing wrong? 我究竟做错了什么?

Judging from the source code excerpts you've provided, there is no reason why it shouldn't work. 从你提供的源代码摘录来看,没有理由不这样做。 I can only make assumptions, but since I'm currently developing a project which uses Dotenv as well, I'd like to point out two possibilities. 我只能做出假设,但由于我目前正在开发一个使用Dotenv的项目,我想指出两种可能性。

Firstly, I'm not sure why you're using the overload() Method. 首先,我不确定你为什么要使用overload()方法。 It's completely sufficient to load Dotenv like this in your public/index.php : public/index.php加载像这样的Dotenv是完全足够的:

define('APP_PATH', realpath('..'));
include APP_PATH . '/vendor/autoload.php';

$dotenv = new Dotenv\Dotenv(APP_PATH);
$dotenv->load();

This brings me to the second and most likely source of your problem: 这让我想到了问题的第二个也是最可能的来源:

You're loading Dotenv in the loader.php file. 您在loader.php文件中加载Dotenv。 In most Phalcon example projects, that one is loaded after the config, therefore your Env variables aren't set at that point yet. 在大多数Phalcon示例项目中,一个在配置之后加载,因此您的Env变量尚未在该点设置。 Include the code I mentioned in your index.php and you should be good. 包括我在index.php提到的代码,你应该很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM