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

  1. Pass an explicit Request: $generator->generate($controller, Request::create('/')).
  2. Call generate() only during request handling.
  3. 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

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


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)