将安全上下文注入(实体侦听器)类时的循环引用 [英] Circular Reference when injecting Security Context into (Entity Listener) Class

查看:142
本文介绍了将安全上下文注入(实体侦听器)类时的循环引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有两个问题说,注入整个服务容器应该解决这个问题。但是问题见下文(注意尝试2和3之间的差异)...



尝试1

  public function __construct(SecurityContext $ securityContext){
$ this-> securityContext = $ securityContext);
}

标准参考。

尝试2

  public function __construct(ContainerInterface $ container){
$ this-> securityContext = $ container-> get('security.context');
}

循环引用(为什么?,我正在注入容器就像尝试3,除了我得到安全上下文)



尝试3

  public function __construct(ContainerInterface $ container){
$ this-> container = $ container;
}

Works。

解决方案

这是因为您的安全上下文依赖于此侦听器,可能通过将实体管理器注入用户提供程序。最好的解决方案是将容器注入侦听器并懒惰地访问安全上下文。



我通常不喜欢将整个容器注入服务,而是将与Doctrine听众的例外是因为他们被迫加载,因此应该尽可能懒惰。


There was 2 questions here saying injecting the whole service container should solve this. But question ... see below (note difference between try 2 & 3) ...

Try 1

public function __construct(SecurityContext $securityContext) {
    $this->securityContext = $securityContext);  
}  

Curcular Reference. Okay ...

Try 2

public function __construct(ContainerInterface $container) {
    $this->securityContext = $container->get('security.context');  
}  

Circular Reference (Why?, I am injecting the container like in try 3 except I got the security context only)

Try 3

public function __construct(ContainerInterface $container) {
    $this->container = $container;  
}  

Works.

解决方案

This happens because your security context depends on this listener, probably via the entity manager being injected into a user provider. The best solution is to inject the container into the listener and access the security context lazily.

I typically don't like injecting the entire container into a service, but make an exception with Doctrine listeners because they are eagerly loaded and should therefore be as lazy as possible.

这篇关于将安全上下文注入(实体侦听器)类时的循环引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆