Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
避免将硬编码数据库凭据与shell-cakephp3结合使用_Php_Shell_Cakephp_Cakephp 3.0 - Fatal编程技术网

避免将硬编码数据库凭据与shell-cakephp3结合使用

避免将硬编码数据库凭据与shell-cakephp3结合使用,php,shell,cakephp,cakephp-3.0,Php,Shell,Cakephp,Cakephp 3.0,我已经编写了一个CakePHP3shell应用程序,它使用模型和行为生成缩略图。由于我通过$\u SERVER super global填充了数据库凭据,因此我需要手动插入凭据。但是,当我尝试以下代码片段时,会出现一个错误,说明我无法覆盖连接: public main($host, $username, $password, $db){ ConnectionManager::config('default', [ 'className' => 'Cake\Databas

我已经编写了一个CakePHP3shell应用程序,它使用模型和行为生成缩略图。由于我通过$\u SERVER super global填充了数据库凭据,因此我需要手动插入凭据。但是,当我尝试以下代码片段时,会出现一个错误,说明我无法覆盖连接:

public main($host, $username, $password, $db){
    ConnectionManager::config('default', [
      'className' => 'Cake\Database\Connection',
      'driver' => 'Cake\Database\Driver\Mysql',
      'persistent' => false,
      'host' => $host,
      'username' => $username,
      'password' => $secret,
      'database' => $db,
      'encoding' => 'utf8',
      'timezone' => 'UTC',
      'cacheMetadata' => true,
    ]);
    ........
}
此外,它首先尝试在config/app.php中加载我的默认设置,这些设置未填充并引发通知和错误:

if (isset($_SERVER['RDS_HOSTNAME']) && isset($_SERVER['RDS_USERNAME']) && isset($_SERVER['RDS_PASSWORD']) && isset($_SERVER['RDS_DB_NAME']) ) {
   define('RDS_HOSTNAME', $_SERVER['RDS_HOSTNAME']);
   define('RDS_USERNAME', $_SERVER['RDS_USERNAME']);
   define('RDS_PASSWORD', $_SERVER['RDS_PASSWORD']);
   define('RDS_DB_NAME', $_SERVER['RDS_DB_NAME']); 
}
return [
.......
    'Datasources' => [
    'default' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => RDS_HOSTNAME,
        /**
         * CakePHP will use the default DB port based on the driver selected
         * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
         * the following line and set the port accordingly
         */
        //'port' => 'non_standard_port_number',
        'username' => RDS_USERNAME,
        'password' => RDS_PASSWORD,
        'database' => RDS_DB_NAME,
        'encoding' => 'utf8',
        'timezone' => 'UTC',

有没有办法避免以这种方式对数据库凭据进行硬编码?

您可以使用环境变量,不熟悉cakephp,但这似乎很有帮助。我将对此进行研究,谢谢:)。您可以使用环境变量,不熟悉cakephp,但这似乎很有帮助。我会留意的,谢谢:)。