当我在我的 symfony2 服务(树枝扩展)中注入 security.context 时,会出现以下错误:
在非对象上调用成员函数 getUser() .....
class GeninnoShareboardExtension extends \Twig_Extension {
public function __construct(ContainerInterface $container, SecurityContext $context) {
$this->doctrine = $container->get('doctrine');
$this->context = $context;
}
public function getUser() {
return $this->context->getToken()->getUser();
}
........
}
我的 services.yml 看起来像这样:
services:
geninno.twig.extension.dashboard:
class: Geninno\EDSBundle\Twig\Extension\GeninnoShareboardExtension
arguments:
container: "@service_container"
service: "@security.context"
tags:
- { name: twig.extension }
用户已登录,我的防火墙设置如下:
access_control:
- { path: ^/secured/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/secured/create, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/secured/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: [IS_AUTHENTICATED_FULLY, IS_AUTHENTICATED_REMEMBERED] }