无法序列化 Symfony\Component\Cache\Adapter\AbstractAdapter

问题描述 投票:0回答:2

我删除了 FosUserBundle 并开发了自己的用户模块。从那时起,当我尝试序列化会话时,就会出现此错误。

$session->set($this->sessionKey, serialize($token));

编辑1:即使我有答案,我也发布了这个问题,因为我花了3天的时间来解决这个问题,它可以帮助其他人(例如我未来的我路过)

编辑2:因为我再次遇到了这个问题(没有FosUserBundle),我感谢“Stefan I”花时间解释他的经历)

symfony fosuserbundle
2个回答
0
投票

自从我在搜索类似问题时偶然发现了这个线程:

确保您不会将

Symfony\Component\Cache\Adapter\AbstractAdapter
的任何实例写入 PHP 会话。

在请求结束时,php 尝试序列化会话,以便在下一个请求时获取它。

Symfony\Component\Cache\Adapter\AbstractAdapter
按设计抛出序列化异常。

在我们的例子中,我们将实用程序类设置为

Object
的属性。该实用程序类持有对 Symfony FileCache 的引用。一旦
Object
添加到
$_SESSION
,会话关闭就会失败,并出现上面所示的异常(因为对象引用了实用程序,实用程序引用了文件缓存)。删除引用使反/序列化再次成为可能。


-1
投票

问题是用户实体在会话中未正确序列化。我必须按如下方式修改我的实体

class User implements UserInterface ,\Serializable
{
    /**
     * {@inheritdoc}
     */
    public function serialize()
    {
        $test = null;
        return serialize([
            $this->password,
            $this->salt,
            $this->username,
            $this->enabled,
            $this->id,
            $this->email,
            $this->roles,
            $this->groups
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function unserialize($serialized)
    {
        $data = unserialize($serialized);

        list(
            $this->password,
            $this->salt,
            $this->username,
            $this->enabled,
            $this->id,
            $this->email,
            $this->roles,
            $this->groups
            ) = $data;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.