{"record":{"id":"c12dfd771fc0bb73","repo":"symfony/http-foundation","slug":"session-has-not-been-set","errorCode":null,"errorMessage":"Session has not been set.","messagePattern":"Session has not been set\\.","errorType":"exception","errorClass":"SessionNotFoundException","httpStatus":null,"severity":"error","filePath":"Request.php","lineNumber":807,"sourceCode":"    public static function getAllowedHttpMethodOverride(): ?array\n    {\n        return self::$allowedHttpMethodOverride;\n    }\n\n    /**\n     * Gets the Session.\n     *\n     * @throws SessionNotFoundException When session is not set properly\n     */\n    public function getSession(): SessionInterface\n    {\n        $session = $this->session;\n        if (!$session instanceof SessionInterface && null !== $session) {\n            $this->setSession($session = $session());\n        }\n\n        if (null === $session) {\n            throw new SessionNotFoundException('Session has not been set.');\n        }\n\n        return $session;\n    }\n\n    /**\n     * Whether the request contains a Session which was started in one of the\n     * previous requests.\n     */\n    public function hasPreviousSession(): bool\n    {\n        // the check for $this->session avoids malicious users trying to fake a session cookie with proper name\n        return $this->hasSession() && $this->cookies->has($this->getSession()->getName());\n    }\n\n    /**\n     * Whether the request contains a Session object.\n     *","sourceCodeStart":789,"sourceCodeEnd":825,"githubUrl":"https://github.com/symfony/http-foundation/blob/5aea19cd678fa4140f6108406f1096de5e9ed6e4/Request.php#L789-L825","documentation":"Request::getSession() returns the session attached to the request. If no session (and no session factory) has been attached via setSession() or a session listener, it throws SessionNotFoundException('Session has not been set.'). HttpFoundation intentionally does not auto-create sessions; the framework's session listener does that.","triggerScenarios":"Calling $request->getSession() on a request created manually (e.g. Request::create() in tests) without calling setSession() first, or on requests handled outside the framework's SessionListener lifecycle; also via hasPreviousSession() indirect access.","commonSituations":"Functional tests that build Request::create('/')->getSession(); controllers hit by requests where sessions are disabled or the session storage service isn't configured; CLI commands/queue workers that simulate requests without the session service.","solutions":["Attach a session first: $request->setSession(new Session(new MockArraySessionStorage())) (or the native storage).","In Symfony apps, ensure framework.session is enabled and routes go through the kernel so SessionListener sets the session.","Call $request->hasSession() or hasPreviousSession() before calling getSession().","In tests, use StaticRequestFactory or KernelTestCase which wires the session automatically."],"exampleFix":"// before\n$session = $request->getSession(); // throws\n\n// after\nif ($request->hasSession()) {\n    $session = $request->getSession();\n}","handlingStrategy":"try-catch","validationCode":"if ($request->hasPreviousSession() || $request->hasSession()) { /* safe to call getSession */ }","typeGuard":"function getOptionalSession(Request $request): ?SessionInterface\n{\n    return $request->hasSession() ? $request->getSession() : null;\n}","tryCatchPattern":"use Symfony\\Component\\HttpFoundation\\Exception\\SessionNotFoundException;\n\ntry {\n    $session = $request->getSession();\n} catch (SessionNotFoundException $e) {\n    $session = null; // or start a session with setSession()\n}","preventionTips":["Check hasSession()/hasPreviousSession() before getSession().","In tests, attach a Session with MockArraySessionStorage via setSession().","Ensure framework.session is enabled so SessionListener populates requests.","Avoid reading sessions in contexts that bypass the kernel (CLI, workers)."],"tags":["session","http","request","symfony"],"backgroundTag":"resource-not-found","analyzedSha":"5aea19cd678fa4140f6108406f1096de5e9ed6e4","analyzedAt":"2026-09-13T01:52:22.855Z","contentChangedAt":"2026-09-13T01:52:22.855Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}