{"record":{"id":"3b0fb6842c12a171","repo":"yiisoft/yii2","slug":"unable-to-locate-view-file-for-view-view-no-ac","errorCode":null,"errorMessage":"Unable to locate view file for view '$view': no active controller.","messagePattern":"Unable to locate view file for view '\\$view': no active controller\\.","errorType":"exception","errorClass":"InvalidCallException","httpStatus":null,"severity":"error","filePath":"framework/base/View.php","lineNumber":184,"sourceCode":"     * the view file corresponding to a relative view name.\n     * @return string the view file path. Note that the file may not exist.\n     * @throws InvalidCallException if a relative view name is given while there is no active context to\n     * determine the corresponding view file.\n     */\n    protected function findViewFile($view, $context = null)\n    {\n        if (strncmp($view, '@', 1) === 0) {\n            // e.g. \"@app/views/main\"\n            $file = Yii::getAlias($view);\n        } elseif (strncmp($view, '//', 2) === 0) {\n            // e.g. \"//layouts/main\"\n            $file = Yii::$app->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');\n        } elseif (strncmp($view, '/', 1) === 0) {\n            // e.g. \"/site/index\"\n            if (Yii::$app->controller !== null) {\n                $file = Yii::$app->controller->module->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');\n            } else {\n                throw new InvalidCallException(\"Unable to locate view file for view '$view': no active controller.\");\n            }\n        } elseif ($context instanceof ViewContextInterface) {\n            $file = $context->getViewPath() . DIRECTORY_SEPARATOR . $view;\n        } elseif (($currentViewFile = $this->getRequestedViewFile()) !== false) {\n            $file = dirname($currentViewFile) . DIRECTORY_SEPARATOR . $view;\n        } else {\n            throw new InvalidCallException(\"Unable to resolve view file for view '$view': no active view context.\");\n        }\n\n        if (pathinfo($file, PATHINFO_EXTENSION) !== '') {\n            return $file;\n        }\n        $path = $file . '.' . $this->defaultExtension;\n        if ($this->defaultExtension !== 'php' && !is_file($path)) {\n            $path = $file . '.php';\n        }\n\n        return $path;","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/base/View.php#L166-L202","documentation":"Thrown by yii\\base\\View::getViewFile() when a view name starts with a single slash (e.g. '/site/index'), which by convention resolves relative to the currently active controller's module view path. If Yii::$app->controller is null there is no active controller to anchor the resolution, so the lookup cannot proceed and an InvalidCallException is raised. Aliased ('@app/...') and '//'-prefixed (app view path) names never hit this branch.","triggerScenarios":"Calling Yii::$app->view->render('/emails/welcome', ...) from a console command, queue job, or during bootstrap when no controller was ever set; unit tests that render views without stubbing a controller; rendering from a cron handler or event subscriber in a web worker between requests.","commonSituations":"Email/view rendering moved from a controller action into a queue job or console command; developers reusing view paths written for controller context ('/site/partials/x') in background code; tests that construct View directly.","solutions":["Use a path alias instead: render('@app/views/site/index', ...) — alias names are controller-independent.","Pass a $context object implementing yii\\base\\ViewContextInterface as the second argument to render(), so relative resolution anchors to its getViewPath().","For console apps that must reuse controller-style paths, set an active controller first or keep shared partials under an aliased directory."],"exampleFix":"// before (inside a console command or queue job)\necho Yii::$app->view->render('/emails/welcome', ['user' => $user]);\n// no active controller in console context -> InvalidCallException\n\n// after\necho Yii::$app->view->render('@app/views/emails/welcome', ['user' => $user]);","handlingStrategy":"validation","validationCode":"// In controller-less contexts (console/queue), never use '/'-prefixed view names\nif (Yii::$app->controller === null) {\n    $viewName = '@app/views' . $viewName; // '/site/index' -> '@app/views/site/index'\n}\nreturn Yii::$app->view->render($viewName, $params, $context);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use '@app/views/...' aliases in all code that can run outside a request (console commands, queue jobs, mailers)","Pass an explicit ViewContextInterface context to render() in services","Add a console smoke test that renders every view used by background jobs"],"tags":["php","yii2","views","rendering","console","controller-context"],"backgroundTag":"missing-view-context","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}