{"record":{"id":"8f70a02495c7f369","repo":"symfony/http-kernel","slug":"generating-a-fragment-url-can-only-be-done-when-handling-a","errorCode":null,"errorMessage":"Generating a fragment URL can only be done when handling a Request.","messagePattern":"Generating a fragment URL can only be done when handling a Request\\.","errorType":"exception","errorClass":"LogicException","httpStatus":null,"severity":"error","filePath":"Fragment/FragmentUriGenerator.php","lineNumber":38,"sourceCode":" * Generates a fragment URI.\n *\n * @author Kévin Dunglas <kevin@dunglas.fr>\n * @author Fabien Potencier <fabien@symfony.com>\n */\nfinal class FragmentUriGenerator implements FragmentUriGeneratorInterface\n{\n    public function __construct(\n        private string $fragmentPath,\n        private ?UriSigner $signer = null,\n        private ?RequestStack $requestStack = null,\n        private \\DateTimeInterface|\\DateInterval|int $expiration = new \\DateInterval('P5Y'),\n    ) {\n    }\n\n    public function generate(ControllerReference $controller, ?Request $request = null, bool $absolute = false, bool $strict = true, bool $sign = true): string\n    {\n        if (null === $request && (null === $this->requestStack || null === $request = $this->requestStack->getCurrentRequest())) {\n            throw new \\LogicException('Generating a fragment URL can only be done when handling a Request.');\n        }\n\n        if ($sign && null === $this->signer) {\n            throw new \\LogicException('You must use a URI when using the ESI rendering strategy or set a URL signer.');\n        }\n\n        if ($strict) {\n            $this->checkNonScalar($controller->attributes);\n        }\n\n        // We need to forward the current _format and _locale values as we don't have\n        // a proper routing pattern to do the job for us.\n        // This makes things inconsistent if you switch from rendering a controller\n        // to rendering a route if the route pattern does not contain the special\n        // _format and _locale placeholders.\n        if (!isset($controller->attributes['_format'])) {\n            $controller->attributes['_format'] = $request->getRequestFormat();\n        }","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/symfony/http-kernel/blob/aa3a39d7286a62cdfea98f0e69c651a3da6e36cf/Fragment/FragmentUriGenerator.php#L20-L56","documentation":"FragmentUriGenerator::generate() throws LogicException when no Request is available (neither passed explicitly nor present on the request stack), because the fragment URL must be built relative to the current request's scheme/host/path.","triggerScenarios":"Calling generate($controller) outside an HTTP request, or with $request = null while the RequestStack is empty.","commonSituations":"Generating fragment URLs in console commands, message handlers, or cache warmers where there is no master request.","solutions":["Pass an explicit Request: $generator->generate($controller, Request::create('/')).","Call generate() only during request handling.","In CLI, configure a request context / build absolute URLs from configured base URL."],"exampleFix":"// before\n$url = $generator->generate($controllerReference);\n// after\n$url = $generator->generate($controllerReference, Request::create('https://example.com/'));","handlingStrategy":"validation","validationCode":"if (null === $request && null === $requestStack?->getCurrentRequest()) { // skip or build absolute URL from configured base }","typeGuard":null,"tryCatchPattern":"try { $url = $generator->generate($ref); } catch (\\LogicException $e) { $url = '/_fragment?'.http_build_query($ref->attributes); }","preventionTips":["Pass an explicit Request when generating outside request scope","Centralize fragment URL generation in a request-aware service","Document that fragment URLs need request context"],"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"}