yiisoft/yii2 · error · InvalidConfigException
Unexpected configuration type for the "$id" component: getty
Error message
Unexpected configuration type for the "$id" component: gettype($definition)
What it means
The other branch of yii\di\ServiceLocator::setComponent()/set(): the definition is neither an object, a valid callable (which includes class-name strings and 'Class::method' syntax), nor an array - i.e. a scalar like an int, bool, float, or non-callable string. The locator enumerates every acceptable type in the message, so any other PHP type is rejected immediately.
Source
Thrown at framework/di/ServiceLocator.php:213
return;
}
if (is_object($definition) || is_callable($definition, true)) {
// an object, a class name, or a PHP callable
$this->_definitions[$id] = $definition;
} elseif (is_array($definition)) {
// a configuration array
if (isset($definition['__class'])) {
$this->_definitions[$id] = $definition;
$this->_definitions[$id]['class'] = $definition['__class'];
unset($this->_definitions[$id]['__class']);
} elseif (isset($definition['class'])) {
$this->_definitions[$id] = $definition;
} else {
throw new InvalidConfigException("The configuration for the \"$id\" component must contain a \"class\" element.");
}
} else {
throw new InvalidConfigException("Unexpected configuration type for the \"$id\" component: " . gettype($definition));
}
}
/**
* Removes the component from the locator.
* @param string $id the component ID
*/
public function clear($id)
{
unset($this->_definitions[$id], $this->_components[$id]);
}
/**
* Returns the list of the component definitions or the loaded component instances.
* @param bool $returnDefinitions whether to return component definitions instead of the loaded component instances.
* @return array the list of the component definitions or the loaded component instances (ID => definition or instance).
*/
public function getComponents($returnDefinitions = true)View on GitHub (pinned to 66f00d18a2)
Solutions
- Wrap scalars into a proper definition array with 'class': ['class' => FileCache::class, 'defaultDuration' => 3600].
- If the intent was to pass an existing object, instantiate it first; if a callable, ensure the syntax is callable (class string or 'Class::method').
- Validate env-derived config before registering components (is_array with a 'class' key).
- Fix the config source so it produces structured arrays, not flattened scalars.
Example fix
// before
Yii::$app->setComponents(['cache' => 3600]);
// after
Yii::$app->setComponents([
'cache' => ['class' => yii\caching\FileCache::class, 'defaultDuration' => 3600],
]); Defensive patterns
Strategy: validation
Validate before calling
if (!is_array($definition) && !is_object($definition) && !is_callable($definition, true)) {
throw new \InvalidArgumentException(
'Component definition must be an object, callable, or config array; got ' . gettype($definition)
);
}
Yii::$app->setComponents([$id => $definition]); Type guard
/** Narrows untrusted config values to the types setComponents() accepts. */
function normalizeDefinition($value): array
{
if (is_array($value)) {
return $value;
}
if (is_object($value) || (is_string($value) && class_exists($value))) {
return ['class' => $value];
}
throw new \UnexpectedValueException('Unsupported component definition type: ' . gettype($value));
} Try / catch
try {
Yii::$app->setComponents($config['components']);
} catch (\yii\base\InvalidConfigException $e) {
if (strpos($e->getMessage(), 'Unexpected configuration type') !== false) {
// a scalar leaked into config - fix at the source (env loader, JSON schema)
throw new \RuntimeException('Malformed components config', 0, $e);
}
throw $e;
} Prevention
- Never put raw option values (numbers, booleans) in the definition slot - wrap them in a config array.
- Validate env/JSON-derived config with a schema check before registering components.
- Unit-test config loading so structural regressions (array collapsed to scalar) fail in CI, not in production.
When it happens
Trigger: Yii::$app->setComponents(['cache' => 3600]) or ['debug' => true]; a definition read from .env/JSON as a scalar; passing a resource or an uncallable string ('some unknown id ') that is not valid callable syntax.
Common situations: Env-driven config where a numeric/boolean placeholder was meant to be expanded into a full component config; copy-paste of option values (duration, flags) into the definition slot; degraded config merges collapsing an array to its first scalar.
Related errors
- Unknown component ID: $id
- The configuration for the "$id" component must contain a "cl
- Invalid validation rule: a rule must specify both attribute
- The directory does not exist: $path
- {cipher} is not an allowed cipher
AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17).
Data as JSON: /api/errors/2cdbbd7ac29927ec.
Report an issue: GitHub.