symfony/http-foundation · error · LogicException

accepts only string as data.

Error message

%s accepts only string as data.

What it means

IdentityMarshaller::marshall() is an identity marshaller: it passes values through unchanged, but only accepts strings (its contract is to serialize strings). It throws LogicException if any value in the $values map is not a string, because such values would be silently mis-stored by the session handler it fronts.

Solutions

  1. Ensure every value written to the session is a string before storage
  2. Replace IdentityMarshaller with a serializing marshaller such as DefaultMarshaller (supports any serializable value)
  3. Cast/serialize objects yourself (serialize(), json_encode()) before assigning to the session
  4. Add a type check in your session-writing code path to catch non-string values early

Example fix

// before
$marshaller = new IdentityMarshaller();
$_SESSION['cart'] = $cartObject; // LogicException at write time
// after
$marshaller = new \Symfony\Component\HttpFoundation\Session\Storage\Handler\MarshallingSessionHandler($handler, new \Symfony\Component\HttpFoundation\Session\Storage\Handler\DefaultMarshaller());
// or store a string
$_SESSION['cart'] = serialize($cartObject);
Defensive patterns

Strategy: validation

Validate before calling

foreach ($_SESSION as $k => $v) {
    if (!is_string($v)) {
        throw new InvalidArgumentException(sprintf('Session key "%s" must be a string when using IdentityMarshaller, got %s.', $k, get_debug_type($v)));
    }
}

Type guard

function allSessionValuesAreStrings(array $values): bool
{
    return array_reduce($values, fn (bool $ok, $v) => $ok && is_string($v), true);
}

Prevention

When it happens

Trigger: Calling marshall() with an array containing any non-string value (int, float, array, object, null). In practice, storing non-scalar session data (e.g. $_SESSION['x'] = new \DateTime()) through a marshalling session handler configured with IdentityMarshaller.

Common situations: Developers putting objects or arrays into session storage while using MarshallingSessionHandler with IdentityMarshaller; forgetting to use a real serializer marshaller (e.g. DefaultMarshaller) for non-string session values.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of symfony/http-foundation@5aea19cd67 (2026-09-13). Data as JSON: /api/errors/d77fedf15f12286c. Report an issue: GitHub.

Appendix: source

Thrown at Session/Storage/Handler/IdentityMarshaller.php:25

 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;

use Symfony\Component\Cache\Marshaller\MarshallerInterface;

/**
 * @author Ahmed TAILOULOUTE <ahmed.tailouloute@gmail.com>
 */
class IdentityMarshaller implements MarshallerInterface
{
    public function marshall(array $values, ?array &$failed): array
    {
        foreach ($values as $key => $value) {
            if (!\is_string($value)) {
                throw new \LogicException(\sprintf('%s accepts only string as data.', __METHOD__));
            }
        }

        return $values;
    }

    public function unmarshall(string $value): string
    {
        return $value;
    }
}

View on GitHub (pinned to 5aea19cd67)