问题描述
我正在使用 Symfony 2.0.16
I'm working over Symfony 2.0.16
我的 UserProvider 中有 getRoles 方法
I have in my UserProvider the method getRoles
public function getRoles()
{
/**
* @var \Doctrine\Common\Collections\ArrayCollection $rol
*/
return $this->rol->toArray();
}
并且我的 Rol 实体具有角色接口
and my Rol entity has the role interface
class Rol implements \Symfony\Component\Security\Core\Role\RoleInterface
//...
public function getRole()
{
return $this->getName();
}
但是当我尝试登录时出现以下错误
but when I try to login I get the following error
致命错误:在线调用 C:\Users\julian\Code\parqueadero\vendor\symfony\src\Symfony\Bundle\SecurityBundle\DataCollector\SecurityDataCollector.php 中非对象的成员函数 getRole()57
读取类SecurityDataCollector,由Closure抛出错误
Reading the class SecurityDataCollector, the error is thrown by a Closure
array_map(function ($role){ return $role->getRole();}, $token->getRoles()
现在我把它改成
array_map(function ($role){ var_dump($role); return $role->getRole();}, $token->getRoles()
令我惊讶的是,$role
是一个对象 Rol,但我不明白为什么会出现错误.
To my surprise, $role
is a object Rol but I can't understand why I get the error.
推荐答案
我发现该问题的解决方案是一个 PHP 5.4 中的错误(我正在使用的 php)序列化方法 github 用户 yoannch 提出了这个 解决方案,使用json_encode/json_decode
方法覆盖serialize/unserialize
方法
I found the solution the problem is a bug in PHP 5.4 (the php i'm using) serialize method the github user yoannch proposed this solution, is overwrite the serialize/unserialize
methods using json_encode/json_decode
methods
class User implements \Serializable
//...
/**
* Serializes the content of the current User object
* @return string
*/
public function serialize()
{
return \json_encode(
array($this->username, $this->password, $this->salt,
$this->rol, $this->id));
}
/**
* Unserializes the given string in the current User object
* @param serialized
*/
public function unserialize($serialized)
{
list($this->username, $this->password, $this->salt,
$this->rol, $this->id) = \json_decode(
$serialized);
}
只需要更改正确的名称属性
only need change the correct name properties
这篇关于RoleInterface 抛出“对非对象的调用";错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!