{"record":{"id":"57a9ef9b0f10329a","repo":"yiisoft/yii2","slug":"the-required-component-is-not-specified","errorCode":null,"errorMessage":"The required component is not specified.","messagePattern":"The required component is not specified\\.","errorType":"exception","errorClass":"InvalidConfigException","httpStatus":null,"severity":"error","filePath":"framework/di/Instance.php","lineNumber":143,"sourceCode":"                $container = Yii::$container;\n            }\n            if (isset($reference['__class'])) {\n                $class = $reference['__class'];\n                unset($reference['__class'], $reference['class']);\n            } elseif (isset($reference['class'])) {\n                $class = $reference['class'];\n                unset($reference['class']);\n            } else {\n                $class = $type;\n            }\n            $component = $container->get($class, [], $reference);\n            if ($type === null || $component instanceof $type) {\n                return $component;\n            }\n\n            throw new InvalidConfigException('Invalid data type: ' . $class . '. ' . $type . ' is expected.');\n        } elseif (empty($reference)) {\n            throw new InvalidConfigException('The required component is not specified.');\n        }\n\n        if (is_string($reference)) {\n            $reference = new static($reference);\n        } elseif ($type === null || $reference instanceof $type) {\n            return $reference;\n        }\n\n        if ($reference instanceof self) {\n            try {\n                $component = $reference->get($container);\n            } catch (\\ReflectionException $e) {\n                throw new InvalidConfigException('Failed to instantiate component or class \"' . $reference->id . '\".', 0, $e);\n            }\n            if ($type === null || $component instanceof $type) {\n                return $component;\n            }\n","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/di/Instance.php#L125-L161","documentation":"yii\\di\\Instance::ensure() received an empty value (empty array, empty string, null, false, 0) where a component reference is required, and throws 'The required component is not specified.' The method accepts a component ID string, a config array, an Instance, or a ready object - none of which can be empty, so there is nothing to resolve.","triggerScenarios":"A typed component property is set to [] or '' in config; a value sourced from an environment variable that is unset (null/false) is passed through; Instance::ensure(getenv('CACHE_ID'), Cache::class) when the variable is not defined.","commonSituations":"Env-driven configuration where a variable is missing in one environment (staging/CI); optional components represented as empty arrays in defaults merged with Yi\\config or array_merge; YAML/JSON config round-trips turning null into ''.","solutions":["Provide a real reference: a component ID string, a config array with a 'class' key, or an object instance.","Default missing env values: $id = getenv('CACHE_ID') ?: 'cache'; before passing it on.","Skip setting the property entirely when the component is not configured, instead of setting it to an empty value.","Audit config merges so later files can't overwrite a valid component config with []."],"exampleFix":"// before\n'cache' => [], // or 'cache' => '' / null from an unset env var\n\n// after\n'cache' => ['class' => yii\\caching\\FileCache::class],\n// or when env-driven\n'cache' => getenv('CACHE_CLASS') ?: yii\\caching\\FileCache::class,","handlingStrategy":"validation","validationCode":"if (empty($reference)) {\n    throw new \\InvalidArgumentException('Component reference must be an ID, config array, or object; got empty value.');\n}\n$component = \\yii\\di\\Instance::ensure($reference, $type);","typeGuard":"/** True when the value is a usable component reference (non-empty ID/config/Instance/object). */\nfunction isNonEmptyReference($value): bool\n{\n    if (is_string($value)) {\n        return $value !== '';\n    }\n    if (is_array($value)) {\n        return $value !== [];\n    }\n    return $value instanceof \\yii\\di\\Instance || is_object($value);\n}","tryCatchPattern":"try {\n    $component = \\yii\\di\\Instance::ensure($value, $type);\n} catch (\\yii\\base\\InvalidConfigException $e) {\n    if (strpos($e->getMessage(), 'The required component is not specified') === 0) {\n        // fall back to a sane default component instead of failing\n        $component = \\yii\\di\\Instance::ensure('cache', $type);\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Default env-derived IDs: $id = getenv('CACHE_ID') ?: 'cache'.","Skip setting component properties that are not configured rather than setting them to null/[]/''.","Assert config completeness in a bootstrap script (required keys present, none empty) before the app serves traffic.","Watch ArrayHelper::merge overwrites that can replace a full config with an empty array."],"tags":["yii2","dependency-injection","config","instance","empty-value"],"backgroundTag":"missing-component-reference","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}