symfony/http-kernel · error · LogicException
Controller attributes cannot contain non-scalar/non-null…
Error message
Controller attributes cannot contain non-scalar/non-null values (value for key "%s" is not a scalar or null).
What it means
FragmentUriGenerator::checkNonScalar() throws LogicException when controller attributes contain arrays nested with non-scalar, non-null values. Fragment attributes are serialized into the URL query string, so only scalars and null survive the round trip.
Solutions
- Pass only scalars (int, string, bool, float) or null as attributes; pass identifiers (e.g. entity id) instead of objects.
- Remove the offending attribute or fetch the object inside the fragment controller.
- Pass $strict = false to skip the check only if you understand the value will be mangled in the URL.
Example fix
// before
$ref = new ControllerReference('App\Controller\FragmentController::show', ['product' => $product]);
// after
$ref = new ControllerReference('App\Controller\FragmentController::show', ['productId' => $product->getId()]); Defensive patterns
Strategy: validation
Validate before calling
foreach ($attributes as $k => $v) { if (!is_scalar($v) && null !== $v) { throw new \InvalidArgumentException("Attribute '$k' must be scalar or null"); } } Type guard
function hasOnlyScalarAttributes(array $attrs): bool { foreach ($attrs as $v) { if (is_array($v)) { return hasOnlyScalarAttributes($v); } if (!is_scalar($v) && null !== $v) { return false; } } return true; } Prevention
- Pass IDs, not entities, to fragment controllers
- Validate ControllerReference attributes in a dev-mode listener
- Remember attributes become URL query parameters
When it happens
Trigger: Passing a ControllerReference whose attributes include objects, resources, or nested arrays holding such values, e.g. generate(new ControllerReference('App\Controller::show', ['entity' => $object])).
Common situations: Passing entities or value objects as fragment controller arguments; forgetting that URL generation flattens attributes to query parameters.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- The " " renderer does not exist.
- Rendering a fragment can only be done when handling a…
- 0
- Generating a fragment URL can only be done when handling a…
- You must use a URI when using the ESI rendering strategy or…
AI-assisted analysis of symfony/http-kernel@aa3a39d728 (2026-09-13).
Data as JSON: /api/errors/374e67b9978e9f69.
Report an issue: GitHub.
Appendix: source
Thrown at Fragment/FragmentUriGenerator.php:83
// we need to sign the absolute URI, but want to return the path only.
$fragmentUri = $sign || $absolute ? $request->getUriForPath($path) : $request->getBaseUrl().$path;
if (!$sign) {
return $fragmentUri;
}
$fragmentUri = $this->signer->sign($fragmentUri, $this->expiration);
return $absolute ? $fragmentUri : substr($fragmentUri, \strlen($request->getSchemeAndHttpHost()));
}
private function checkNonScalar(array $values): void
{
foreach ($values as $key => $value) {
if (\is_array($value)) {
$this->checkNonScalar($value);
} elseif (!\is_scalar($value) && null !== $value) {
throw new \LogicException(\sprintf('Controller attributes cannot contain non-scalar/non-null values (value for key "%s" is not a scalar or null).', $key));
}
}
}
}
View on GitHub (pinned to aa3a39d728)