cakephp/cakephp · error · Cake\Core\Exception\CakeException

The ` ` has already been loaded.

Error message

The `%s` has already been loaded.

What it means

ObjectRegistry::_checkDuplicate() detects loading the same alias twice. If the previously loaded object does not implement getConfig(), the registry cannot compare configurations, so it throws immediately — reloading an alias with a different class/config cannot be reconciled safely.

Solutions

  1. Remove the duplicate load() call, or the duplicate entry in the $helpers/$components property array
  2. Unload first with $registry->unload($name) before re-loading with different config
  3. Check for alias collisions between plugins and core (use a unique alias with 'className' => RealClass)
  4. Upgrade legacy objects to implement ConfigTrait/getConfig() so config comparison works

Example fix

// before
class AppController {
    public $helpers = ['Form'];
    public function initialize() { $this->loadHelper('Form'); } // duplicate
}

// after
class AppController {
    public $helpers = ['Form']; // loaded once, or use unload() before reload
}
Defensive patterns

Strategy: try-catch

Validate before calling

if ($registry->has($name)) {
    $registry->unload($name);
}
$registry->load($name, $config);

Try / catch

try {
    $registry->load($name, $config);
} catch (CakeException $e) {
    error_log('Duplicate load: ' . $e->getMessage());
}

Prevention

When it happens

Trigger: Calling $registry->load('HelperName') twice where the first loaded object lacks a getConfig() method (legacy/plain object); loading a helper/component alias that collides with an already-loaded non-configurable object.

Common situations: Plugins that map an alias to a custom class while core already loaded the same alias; duplicate ->load() calls in both a controller's $helpers property and its initialize(); old third-party helpers without the getConfig() API.

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


AI-assisted analysis of cakephp/cakephp@1128eba9b0 (2026-09-12). Data as JSON: /api/errors/660aba584417cf64. Report an issue: GitHub.

Appendix: source

Thrown at src/Core/ObjectRegistry.php:139

     * bad and an exception will be raised.
     *
     * An exception is raised, as replacing the object will not update any
     * references other objects may have. Additionally, simply updating the runtime
     * configuration is not a good option as we may be missing important constructor
     * logic dependent on the configuration.
     *
     * @param string $name The name of the alias in the registry.
     * @param array<string, mixed> $config The config data for the new instance.
     * @return void
     * @throws \Cake\Core\Exception\CakeException When a duplicate is found.
     */
    protected function _checkDuplicate(string $name, array $config): void
    {
        $existing = $this->_loaded[$name];
        $msg = sprintf('The `%s` alias has already been loaded.', $name);
        $hasConfig = method_exists($existing, 'getConfig');
        if (!$hasConfig) {
            throw new CakeException($msg);
        }
        if (!$config) {
            return;
        }
        $existingConfig = $existing->getConfig();
        unset($config['enabled'], $existingConfig['enabled']);

        $failure = null;
        foreach ($config as $key => $value) {
            if (!array_key_exists($key, $existingConfig)) {
                $failure = " The `{$key}` was not defined in the previous configuration data.";
                break;
            }
            if (isset($existingConfig[$key]) && $existingConfig[$key] !== $value) {
                $failure = sprintf(
                    ' The `%s` key has a value of `%s` but previously had a value of `%s`',
                    $key,
                    json_encode($value, JSON_THROW_ON_ERROR),

View on GitHub (pinned to 1128eba9b0)