Symfony / Silex SecurityServiceProvider:不允许序列化'Closure'

时间:2016-07-28 12:54:10

标签: php forms symfony authentication silex

我遵循[Silex SecurityServiceProvider] [1]

[1]:http://silex.sensiolabs.org/doc/providers/security.html一步一步,但是当我提交登录表单时,我收到错误:

Exception in AbstractToken.php line 154:
Serialization of 'Closure' is not allowed

我不知道这个错误来自哪里......我使用与例子完全相同的代码:

控制器:

$app->get('/login', function(Request $request) use ($app) {
    return $app['twig']->render('login.twig', array(
        'error'         => $app['security.last_error']($request),
        'last_username' => $app['session']->get('_security.last_username'),
    ));
});

视图:

<form action="{{ path('connexion') }}" method="post">
    {{ error }}
    <input type="text" name="_username" value="{{ last_username }}" />
    <input type="password" name="_password" value="" />
    <input type="submit" />
</form>

错误的第一行:

 in AbstractToken.php line 154
at SplObjectStorage->serialize()
at serialize(array(object(User), true, array(object(Role)), array())) in AbstractToken.php line 154
at AbstractToken->serialize() in UsernamePasswordToken.php line 96
at UsernamePasswordToken->serialize()
at serialize(object(UsernamePasswordToken)) in ContextListener.php line 129
at ContextListener->onKernelResponse(object(FilterResponseEvent), 'kernel.response', object(TraceableEventDispatcher))
at call_user_func(array(object(ContextListener), 'onKernelResponse'), object(FilterResponseEvent), 'kernel.response', object(TraceableEventDispatcher)) in WrappedListener.php line 61
at WrappedListener->__invoke(object(FilterResponseEvent), 'kernel.response', object(EventDispatcher))
at call_user_func(object(WrappedListener), object(FilterResponseEvent), 'kernel.response', object(EventDispatcher)) in EventDispatcher.php line 174

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我通过implements \ Serializable解决了这个问题。

class User extends Entity implements UserInterface, \Serializable {

根据文件

http://symfony.com/doc/current/cookbook/security/entity_provider.html