symfony/http-kernel · error · LogicException
Generating a fragment URL can only be done when handling a…
Error message
Generating a fragment URL can only be done when handling a Request.
What it means
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.
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.
Example fix
// before
$url = $generator->generate($controllerReference);
// after
$url = $generator->generate($controllerReference, Request::create('https://example.com/')); Defensive patterns
Strategy: validation
Validate before calling
if (null === $request && null === $requestStack?->getCurrentRequest()) { // skip or build absolute URL from configured base } Try / catch
try { $url = $generator->generate($ref); } catch (\LogicException $e) { $url = '/_fragment?'.http_build_query($ref->attributes); } Prevention
- 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
When it happens
Trigger: Calling generate($controller) outside an HTTP request, or with $request = null while the RequestStack is empty.
Common situations: Generating fragment URLs in console commands, message handlers, or cache warmers where there is no master request.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- Rendering a fragment can only be done when handling a…
- The " " renderer does not exist.
- 0
- You must use a URI when using the ESI rendering strategy or…
- Controller attributes cannot contain non-scalar/non-null…
AI-assisted analysis of symfony/http-kernel@aa3a39d728 (2026-09-13).
Data as JSON: /api/errors/8f70a02495c7f369.
Report an issue: GitHub.
Appendix: source
Thrown at Fragment/FragmentUriGenerator.php:38
* Generates a fragment URI.
*
* @author Kévin Dunglas <kevin@dunglas.fr>
* @author Fabien Potencier <fabien@symfony.com>
*/
final class FragmentUriGenerator implements FragmentUriGeneratorInterface
{
public function __construct(
private string $fragmentPath,
private ?UriSigner $signer = null,
private ?RequestStack $requestStack = null,
private \DateTimeInterface|\DateInterval|int $expiration = new \DateInterval('P5Y'),
) {
}
public function generate(ControllerReference $controller, ?Request $request = null, bool $absolute = false, bool $strict = true, bool $sign = true): string
{
if (null === $request && (null === $this->requestStack || null === $request = $this->requestStack->getCurrentRequest())) {
throw new \LogicException('Generating a fragment URL can only be done when handling a Request.');
}
if ($sign && null === $this->signer) {
throw new \LogicException('You must use a URI when using the ESI rendering strategy or set a URL signer.');
}
if ($strict) {
$this->checkNonScalar($controller->attributes);
}
// We need to forward the current _format and _locale values as we don't have
// a proper routing pattern to do the job for us.
// This makes things inconsistent if you switch from rendering a controller
// to rendering a route if the route pattern does not contain the special
// _format and _locale placeholders.
if (!isset($controller->attributes['_format'])) {
$controller->attributes['_format'] = $request->getRequestFormat();
}View on GitHub (pinned to aa3a39d728)