无法在symfony2控制器中获取当前登录的用户

Ran*_*ngh 0 php symfony doctrine-orm

我在控制器中有这个代码

public function newAction()
    {
$em = $this->getDoctrine()->getEntityManager();
$user = $this->container->get('security.context')->getToken()->getUser();
Run Code Online (Sandbox Code Playgroud)

我收到了这个错误

致命错误:在第86行的/home/xxxxx/Acme/UserBundle/Controller/UserController.php中的非对象上调用成员函数getUser()

Vit*_*ian 5

这意味着您的安全上下文未填充令牌.当您的行动路线不在防火墙后面时,就会发生这种情况.

您应该检查您的防火墙模式 security.yml