Drupal 8 Settings.php

时间:2019-06-12 13:42:13

标签: php drupal-8

我正在尝试将环境变量传递给Drupal中用于数据库配置的settings.php文件,我能够访问环境变量并将其存储到变量中,并将这些变量传递给settings.php中的数据库数组

如果我将阵列打印到控制台,则可以正常打印,但是在访问网站Apache日志时出现以下错误

我尝试使用getenv()直接传递env变量 将env变量传递给另一个变量,然后将该新变量传递给数据库数组 将变量转换为UTF-8编码并将其传递到数据库数组

如果我对数据库配置值进行硬编码,则可以正常工作,但是当我尝试通过env变量读取它时,它将无法工作!我无法理解为什么值传递时不能正常工作

错误日志堆栈

Drupal\\Core\\Database\\DatabaseAccessDeniedException: SQLSTATE[HY000] [1045] Access denied for user ''@'localhost' (using password: NO) in /var/www/html/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php on line 427

Settings.php中的代码

$vars = array(
  'DB_HOST',
  'DB_USERNAME',
  'DB_PASSWORD',
  'DB_PORT'
);
foreach($vars as $var) {
  if(!isset($_ENV[$var]) && getenv($var)){
    $_ENV[$var] = getenv($var);
  }
}

$username = mb_convert_encoding($_ENV['DB_USERNAME'], "UTF-8");
$password = mb_convert_encoding($_ENV['DB_PASSWORD'], "UTF-8");
$host = mb_convert_encoding($_ENV['DB_HOST'], "UTF-8");
$port = mb_convert_encoding($_ENV['DB_PORT'], "UTF-8");

$databases['default']['default'] = array (
  'database' => 'a_16a7f4a6-74a2-4066-aaa6-c0f4029f9a9f',
  'username' => $username,
  'password' => $password,
  'prefix' => '',
  'host' => $host,
  'port' => $port,
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);
print_r($databases);

我刚运行php文件时显示的输出

Array
(
  [default] => Array
    (
        [default] => Array
            (
                [database] => a_16a7f4a6-74a2-4066-aaa6-c0f4029f9a9f
                [username] => root
                [password] => root
                [prefix] => 
                [host] => localhost
                [port] => 3306
                [namespace] => Drupal\Core\Database\Driver\mysql
                [driver] => mysql
            )

    )

)

0 个答案:

没有答案