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 in

View on GitHub (pinned to 66f00d18a2)

Solutions

  1. Supply the missing argument: pass it by name in the params array, e.g. invoke($callable, ['to' => ..., 'message' => ...]).
  2. Give the parameter a default value or make it optional in the callable's signature.
  3. If the parameter is class-typed, type-hint it so the container autowires it instead of demanding a scalar.
  4. 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

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


AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17). Data as JSON: /api/errors/3370e09a628d66b9. Report an issue: GitHub.