yiisoft/yii2 · error · InvalidConfigException
Missing required parameter "$name" when calling "$funcName".
Error message
Missing required parameter "$name" when calling "$funcName".
What it means
yii\di\Container throws this InvalidConfigException from resolveCallableDependencies() while building arguments for Container::invoke() (or any callable resolved through the container). A non-class-typed parameter that is not supplied by name (associative call) or positionally, has no default value, and is not optional, cannot be resolved - class-typed parameters are autowired, but scalars never are - so the container reports exactly which parameter of which function is missing.
Source
Thrown at framework/di/Container.php:733
$args[] = $this->get($className);
} catch (NotInstantiableException $e) {
if ($param->isDefaultValueAvailable()) {
$args[] = $param->getDefaultValue();
} else {
throw $e;
}
}
}
} elseif ($associative && isset($params[$name])) {
$args[] = $params[$name];
unset($params[$name]);
} elseif (!$associative && count($params)) {
$args[] = array_shift($params);
} elseif ($param->isDefaultValueAvailable()) {
$args[] = $param->getDefaultValue();
} elseif (!$param->isOptional()) {
$funcName = $reflection->getName();
throw new InvalidConfigException("Missing required parameter \"$name\" when calling \"$funcName\".");
}
}
foreach ($params as $value) {
$args[] = $value;
}
return $args;
}
/**
* Registers class definitions within this container.
*
* @param array $definitions array of definitions. There are two allowed formats of array.
* The first format:
* - key: class name, interface name or alias name. The key will be passed to the [[set()]] method
* as a first argument `$class`.
* - value: the definition associated with `$class`. Possible values are described inView on GitHub (pinned to 66f00d18a2)
Solutions
- Supply the missing argument: pass it by name in the params array, e.g. invoke($callable, ['to' => ..., 'message' => ...]).
- Give the parameter a default value or make it optional in the callable's signature.
- If the parameter is class-typed, type-hint it so the container autowires it instead of demanding a scalar.
- Check spelling/case of keys in an associative params array against the real parameter names.
Example fix
// before Yii::$container->invoke([$service, 'send'], ['to' => 'a@b.c']); // send(string $to, string $message) -> Missing required parameter "message" // after Yii::$container->invoke([$service, 'send'], ['to' => 'a@b.c', 'message' => 'Hello']); // or make it optional in the callee public function send(string $to, string $message = ''): void
Defensive patterns
Strategy: validation
Validate before calling
use yii\di\Container;
$reflection = new \ReflectionMethod($service, 'send');
$params = ['to' => 'a@b.c'];
foreach ($reflection->getParameters() as $p) {
if (!$p->isOptional() && !isset($params[$p->getName()]) && $p->getType() !== null && $p->getType()->isBuiltin()) {
$params[$p->getName()] = /* supply a value */ '';
}
}
Yii::$container->invoke([$service, 'send'], $params); Type guard
/** True when every non-optional scalar param of the callable is supplied. */
function paramsComplete(callable $fn, array $params): bool
{
$ref = is_array($fn) ? new \ReflectionMethod($fn[0], $fn[1]) : new \ReflectionFunction(\Closure::fromCallable($fn));
foreach ($ref->getParameters() as $p) {
$builtin = $p->getType() === null || $p->getType()->isBuiltin();
if ($builtin && !$p->isOptional() && !array_key_exists($p->getName(), $params)) {
return false;
}
}
return true;
} Try / catch
use yii\base\InvalidConfigException;
try {
Yii::$container->invoke([$service, 'send'], $params);
} catch (InvalidConfigException $e) {
if (strpos($e->getMessage(), 'Missing required parameter') === 0) {
// log the callable + params, surface a 422 to the caller
} else {
throw $e;
}
} Prevention
- Give invoke()-target callables sensible defaults or make added parameters optional.
- Use named keys in the params array that exactly match parameter names (they are case-sensitive).
- After changing a method signature, grep for invoke( call sites passing positional/named params.
- Type-hint injectable dependencies so the container autowires them instead of demanding scalars.
When it happens
Trigger: Yii::$container->invoke([$service, 'send'], ['to' => 'a@b.c']) where send(string $to, string $message) is called without 'message'; invoking a closure via the container with a required scalar argument left out; passing an associative array whose keys don't match the parameter names.
Common situations: Refactoring a method signature to add a required parameter and forgetting to update every invoke() call site; typos in parameter-name keys of the $params array ('userName' vs 'name'); switching a positional call to named arguments with wrong keys.
Related errors
- Invalid data type: $valueType. $type is expected.
- Invalid data type: $class. $type is expected.
- The required component is not specified.
- Failed to instantiate component or class "$reference->id".
- "$reference->id" refers to a get_class($component) component
AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17).
Data as JSON: /api/errors/3370e09a628d66b9.
Report an issue: GitHub.