cakephp/cakephp · error · BadMethodCallException
Cannot reconfigure existing key
Error message
Cannot reconfigure existing key `%s`.
What it means
StaticConfigTrait::setConfig() forbids overwriting an existing configuration key and throws BadMethodCallException with this message. Configurations are write-once; to change a config you must drop it first (dropConfig) or use a different key.
Solutions
- Call static::dropConfig('key') before re-configuring, or use a unique key name.
- Guard with if (!static::getConfig('key', null)) before setting.
- In tests, reset config in tearDown(): ClassName::dropConfig('key').
- Remove the duplicate setConfig call (search bootstrap/plugins for the alias).
Example fix
// before
Cache::setConfig('default', $config);
// later...
Cache::setConfig('default', $otherConfig);
// after
Cache::dropConfig('default');
Cache::setConfig('default', $otherConfig); Defensive patterns
Strategy: validation
Validate before calling
if (Cache::getConfig('default') !== null) {
Cache::dropConfig('default');
}
Cache::setConfig('default', $config); Try / catch
try {
Cache::setConfig('default', $cfg);
} catch (\BadMethodCallException $e) {
Cache::dropConfig('default');
Cache::setConfig('default', $cfg);
} Prevention
- Grep bootstrap/plugins for duplicate alias definitions
- In tests, call dropConfig for every alias you setConfig in setUp()
- Use unique alias names per plugin to avoid collisions
When it happens
Trigger: Calling setConfig('default', ...) twice on the same alias during a single request/bootstrap — e.g., application config then plugin bootstrap both configure the same key, or tests re-configuring between test cases without cleanup.
Common situations: Duplicated datasource/log/email aliases defined in app.php and again in bootstrap; test suites that call setConfig in setUp() without tearDown() calling dropConfig(); two plugins configuring the same cache engine.
Understand the failure class
Background: Conflicting config options: "cannot be used together" — configuration validation errors across open-source libraries — this error's family across 162 libraries.
Related errors
- Expected configuration
- If config is not null, key must be a string.
- If config is null, key must be an array.
- Salt not set. Use Security::setSalt() to set one, ideally…
- A validation rule with the name
AI-assisted analysis of cakephp/cakephp@1128eba9b0 (2026-09-12).
Data as JSON: /api/errors/72309247d2e72493.
Report an issue: GitHub.
Appendix: source
Thrown at src/Core/StaticConfigTrait.php:93
*/
public static function setConfig(array|string $key, mixed $config = null): void
{
if ($config === null) {
if (!is_array($key)) {
throw new LogicException('If config is null, key must be an array.');
}
foreach ($key as $name => $settings) {
static::setConfig((string)$name, $settings);
}
return;
}
if (!is_string($key)) {
throw new LogicException('If config is not null, key must be a string.');
}
if (isset(static::$_config[$key])) {
throw new BadMethodCallException(sprintf('Cannot reconfigure existing key `%s`.', $key));
}
if (is_object($config)) {
$config = ['className' => $config];
}
if (is_array($config) && isset($config['url'])) {
$parsed = static::parseDsn($config['url']);
unset($config['url']);
$config = $parsed + $config;
}
if (isset($config['engine']) && empty($config['className'])) {
$config['className'] = $config['engine'];
unset($config['engine']);
}
static::$_config[$key] = $config;View on GitHub (pinned to 1128eba9b0)