我编写了一个Cakephp 3 shell应用程序,它使用模型和行为来生成缩略图。由于我通过$ _SERVER超级全局填充数据库凭据,我需要手动插入凭据。但是,当我尝试以下代码片段时,会导致错误,说我无法覆盖连接:
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',
有没有办法以这种方式避免数据库凭证的硬编码?