{"record":{"id":"7737c184eb201f44","repo":"yiisoft/yii2","slug":"the-configuration-for-the-id-component-must-con","errorCode":null,"errorMessage":"The configuration for the \"$id\" component must contain a \"class\" element.","messagePattern":"The configuration for the \"\\$id\" component must contain a \"class\" element\\.","errorType":"exception","errorClass":"InvalidConfigException","httpStatus":null,"severity":"error","filePath":"framework/di/ServiceLocator.php","lineNumber":210,"sourceCode":"\n        if ($definition === null) {\n            unset($this->_definitions[$id]);\n            return;\n        }\n\n        if (is_object($definition) || is_callable($definition, true)) {\n            // an object, a class name, or a PHP callable\n            $this->_definitions[$id] = $definition;\n        } elseif (is_array($definition)) {\n            // a configuration array\n            if (isset($definition['__class'])) {\n                $this->_definitions[$id] = $definition;\n                $this->_definitions[$id]['class'] = $definition['__class'];\n                unset($this->_definitions[$id]['__class']);\n            } elseif (isset($definition['class'])) {\n                $this->_definitions[$id] = $definition;\n            } else {\n                throw new InvalidConfigException(\"The configuration for the \\\"$id\\\" component must contain a \\\"class\\\" element.\");\n            }\n        } else {\n            throw new InvalidConfigException(\"Unexpected configuration type for the \\\"$id\\\" component: \" . gettype($definition));\n        }\n    }\n\n    /**\n     * Removes the component from the locator.\n     * @param string $id the component ID\n     */\n    public function clear($id)\n    {\n        unset($this->_definitions[$id], $this->_components[$id]);\n    }\n\n    /**\n     * Returns the list of the component definitions or the loaded component instances.\n     * @param bool $returnDefinitions whether to return component definitions instead of the loaded component instances.","sourceCodeStart":192,"sourceCodeEnd":228,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/di/ServiceLocator.php#L192-L228","documentation":"yii\\di\\ServiceLocator::setComponent()/set() only accepts an object, a callable, or a configuration array - and a configuration array must carry the concrete class to instantiate, given as 'class' (or '__class', which is normalized to 'class'). When the array has neither key, the locator cannot know what to build and throws this InvalidConfigException.","triggerScenarios":"Yii::$app->setComponents(['mailer' => ['transport' => 'smtp']]) - options without 'class'; passing a plain settings array (['host' => ..., 'port' => ...]) from env files straight into setComponents(); merging configs where the entry carrying 'class' gets overwritten.","commonSituations":"Config assembled from multiple files/env sources where the 'class' key is lost; overriding a component with only its options (correct in per-environment overrides only when merged with a base that has 'class'); typos like 'Class' or 'className' instead of 'class'.","solutions":["Add the class key: ['class' => yii\\swiftmailer\\Mailer::class, ...options].","Use '__class' only if you need Yii 3-style config; the locator normalizes it to 'class'.","Check config merge order so the file containing 'class' is loaded and not overwritten wholesale.","Verify the key spelling is exactly lowercase 'class'."],"exampleFix":"// before\nYii::$app->setComponents([\n    'mailer' => ['transport' => 'smtp'], // no class\n]);\n\n// after\nYii::$app->setComponents([\n    'mailer' => ['class' => yii\\swiftmailer\\Mailer::class, 'transport' => 'smtp'],\n]);","handlingStrategy":"validation","validationCode":"foreach ($components as $id => $definition) {\n    if (is_array($definition) && !isset($definition['class'], $definition['__class'])) {\n        throw new \\InvalidArgumentException(\"Component '$id' config must contain a 'class' element.\");\n    }\n}\nYii::$app->setComponents($components);","typeGuard":"/** True when the definition can be registered with setComponents(). */\nfunction isRegistrableDefinition($definition): bool\n{\n    if (is_object($definition) || is_callable($definition, true)) {\n        return true;\n    }\n    return is_array($definition) && (isset($definition['class']) || isset($definition['__class']));\n}","tryCatchPattern":"try {\n    Yii::$app->setComponents($config['components']);\n} catch (\\yii\\base\\InvalidConfigException $e) {\n    // message names the offending component ID - fail fast at boot with a clear config error\n    Yii::error($e->getMessage(), 'config');\n    throw;\n}","preventionTips":["Always author component config starting from 'class' => TargetClass::class.","Validate merged config arrays (class key present) in a pre-deploy check.","Remember per-environment overrides must merge (ArrayHelper::merge) with a base that carries 'class', not replace it.","Use exactly the lowercase key 'class' (or '__class'); 'Class'/'className' are silently ignored."],"tags":["yii2","service-locator","config","component","missing-class-key"],"backgroundTag":"missing-class-in-config","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}