{"id":"d2fae20496c08649","repo":"laravel/framework","slug":"unable-to-detect-application-namespace","errorCode":null,"errorMessage":"Unable to detect application namespace.","messagePattern":"Unable to detect application namespace\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"critical","filePath":"src/Illuminate/Foundation/Application.php","lineNumber":1741,"sourceCode":"     * @throws \\RuntimeException\n     */\n    public function getNamespace()\n    {\n        if (! is_null($this->namespace)) {\n            return $this->namespace;\n        }\n\n        $composer = json_decode(file_get_contents($this->basePath('composer.json')), true);\n\n        foreach ((array) data_get($composer, 'autoload.psr-4') as $namespace => $path) {\n            foreach ((array) $path as $pathChoice) {\n                if (realpath($this->path()) === realpath($this->basePath($pathChoice))) {\n                    return $this->namespace = $namespace;\n                }\n            }\n        }\n\n        throw new RuntimeException('Unable to detect application namespace.');\n    }\n}\n","sourceCodeStart":1723,"sourceCodeEnd":1744,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Foundation/Application.php#L1723-L1744","documentation":"Thrown by Application::getNamespace() when it cannot infer the application namespace from composer.json. The method reads basePath/composer.json, walks autoload.psr-4 entries, and matches one whose realpath equals the app/ directory (Application::path()). If composer.json is unreadable/malformed or no PSR-4 entry maps to app/, detection fails. This typically breaks bootstrapping or any 'make:' command that needs the root namespace (e.g. make:controller, make:model).","triggerScenarios":"Running artisan make:* commands, anything calling app_path() or $app->getNamespace(), or bootstrapping a custom Laravel app where composer.json was renamed, moved, or stripped of PSR-4 autoload. Also triggered by incorrect basePath (e.g. running Artisan from a subdirectory or a framework test harness without a proper composer.json).","commonSituations":"Renamed the app/ directory without updating composer.json. Set a custom namespace in composer.json but forgot to run composer dump-autoload. Copied framework src/ without a project composer.json. Bootstrap path/basePath misconfigured. Using a non-standard skeleton that omits autoload.psr-4.","solutions":["Ensure composer.json at the project root contains \"autoload\": { \"psr-4\": { \"App\\\\\": \"app/\" } } matching your app/ directory.","Run `composer dump-autoload` after any composer.json autoload change.","Confirm the app/ directory exists relative to basePath and realpath() resolves it (no broken symlinks / open_basedir restrictions).","Verify basePath is set correctly in bootstrap/app.php and you are running Artisan from the project root.","If you renamed the app directory, update the PSR-4 path value to match (e.g. \"App\\\\\": \"src/\")."],"exampleFix":"// before — composer.json missing/incorrect autoload, `php artisan make:controller Foo` throws\n\n// after — composer.json\n{\n    \"autoload\": {\n        \"psr-4\": {\n            \"App\\\\\": \"app/\"\n        }\n    }\n}\n// then run:\n// composer dump-autoload","handlingStrategy":"validation","validationCode":"$composerPath = base_path('composer.json');\nif (! file_exists($composerPath)) {\n    throw new \\RuntimeException('Missing composer.json at: '.$composerPath);\n}\n$autoload = json_decode(file_get_contents($composerPath), true)['autoload']['psr-4'] ?? [];\n$appPath = realpath(app_path());\n$ok = false;\nforeach ($autoload as $namespace => $paths) {\n    foreach ((array) $paths as $p) {\n        if (realpath(base_path($p)) === $appPath) { $ok = true; break 2; }\n    }\n}\nif (! $ok) {\n    throw new \\RuntimeException('composer.json PSR-4 does not map to app/ — namespace detection will fail.');\n}","typeGuard":"// Namespace detection is a runtime string operation, not a type; guard with the pre-check above before invoking make:* or getNamespace().","tryCatchPattern":"try {\n    $namespace = app()->getNamespace();\n} catch (\\RuntimeException $e) {\n    if (str_contains($e->getMessage(), 'Unable to detect application namespace')) {\n        // run composer dump-autoload or fix PSR-4 mapping\n    }\n    throw $e;\n}","preventionTips":["Keep \"App\\\\\": \"app/\" in composer.json PSR-4 and run composer dump-autoload after changes.","Run artisan from the project root; verify basePath in bootstrap/app.php.","Add a CI step asserting composer.json has a PSR-4 entry matching app/."],"tags":["bootstrap","composer","namespace","psr-4","application"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}