{"record":{"id":"dd4f12a88fd3c5e7","repo":"symfony/http-kernel","slug":"rendering-a-fragment-can-only-be-done-when-handling-a","errorCode":null,"errorMessage":"Rendering a fragment can only be done when handling a Request.","messagePattern":"Rendering a fragment can only be done when handling a Request\\.","errorType":"exception","errorClass":"LogicException","httpStatus":null,"severity":"error","filePath":"Fragment/FragmentHandler.php","lineNumber":78,"sourceCode":"     * Available options:\n     *\n     *  * ignore_errors: true to return an empty string in case of an error\n     *\n     * @throws \\InvalidArgumentException when the renderer does not exist\n     * @throws \\LogicException           when no main request is being handled\n     */\n    public function render(string|ControllerReference $uri, string $renderer = 'inline', array $options = []): ?string\n    {\n        if (!isset($options['ignore_errors'])) {\n            $options['ignore_errors'] = !$this->debug;\n        }\n\n        if (!isset($this->renderers[$renderer])) {\n            throw new \\InvalidArgumentException(\\sprintf('The \"%s\" renderer does not exist.', $renderer));\n        }\n\n        if (!$request = $this->requestStack->getCurrentRequest()) {\n            throw new \\LogicException('Rendering a fragment can only be done when handling a Request.');\n        }\n\n        return $this->deliver($this->renderers[$renderer]->render($uri, $request, $options));\n    }\n\n    /**\n     * Delivers the Response as a string.\n     *\n     * When the Response is a StreamedResponse, the content is streamed immediately\n     * instead of being returned.\n     *\n     * @return string|null The Response content or null when the Response is streamed\n     *\n     * @throws \\RuntimeException when the Response is not successful\n     */\n    protected function deliver(Response $response): ?string\n    {\n        if (!$response->isSuccessful()) {","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/symfony/http-kernel/blob/aa3a39d7286a62cdfea98f0e69c651a3da6e36cf/Fragment/FragmentHandler.php#L60-L96","documentation":"FragmentHandler::render() throws LogicException when the request stack has no current Request. Fragment rendering works by issuing a sub-request derived from the master request, so it is meaningless outside of an HTTP request lifecycle (e.g. CLI commands, workers).","triggerScenarios":"Calling render() from a console command, queue worker, or during kernel.boot before any request is pushed onto the RequestStack.","commonSituations":"Rendering fragments in CLI-generated emails, cache warmers, or tests that forgot to push a Request onto the stack.","solutions":["Only render fragments within the request/response cycle.","In tests, push a Request: $requestStack->push(new Request()) before calling render().","For CLI contexts, render the content another way or construct a fake master Request."],"exampleFix":"// before (in a command)\n$html = $handler->render('/_fragment/foo', 'inline');\n// after (in a test/setup)\n$requestStack->push(Request::create('/'));\n$html = $handler->render('/_fragment/foo', 'inline');","handlingStrategy":"validation","validationCode":"if (null === $requestStack->getCurrentRequest()) { throw new \\LogicException('No active request; cannot render fragments.'); }","typeGuard":null,"tryCatchPattern":"try { $html = $handler->render($uri, 'inline'); } catch (\\LogicException $e) { $html = ''; }","preventionTips":["Only call fragment APIs inside controller/kernel request scope","Guard CLI code paths explicitly","In tests, push a Request onto the stack in setUp"],"tags":["php","symfony","fragment","request-context"],"backgroundTag":"invalid-state-transition","analyzedSha":"aa3a39d7286a62cdfea98f0e69c651a3da6e36cf","analyzedAt":"2026-09-13T18:03:36.509Z","contentChangedAt":"2026-09-13T18:03:36.509Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}