Symfony2-使用注释自定义postFlush方法的参数



我想在Proposals服务的postFlush方法中使用当前用户,所以我刚刚添加了SecurityContextInterface $securityContext作为__construct方法的参数和Proposals服务类的属性:

use SymfonyComponentSecurityCoreSecurityContextInterface;
/**
 * @DIService("pro.proposals")
 * @DITag("doctrine.event_listener", attributes = {"event": "onFlush", "lazy": true})
 * @DITag("doctrine.event_listener", attributes = {"event": "postFlush", "lazy": true})
 */
class Proposals
{
    private $doctrine;
    private $translator;
    private $templating;
    private $notifications;
    private $proposalsWithTypeChanged;
    private $securityContext;

    /**
     * @DIInjectParams({"notifications" = @DIInject("pro.notifications")})
     */
    function __construct(Registry $doctrine, TranslatorInterface $translator, Notifications $notifications, Templating $templating, SecurityContextInterface $securityContext)
    {
        $this->doctrine = $doctrine;
        $this->translator = $translator;
        $this->templating = $templating;
        $this->notifications = $notifications;
        $this->securityContext = $securityContext;
    }

但这给了我一个错误:

ServiceNotFoundException:服务"pro.products"依赖于不存在的服务"security_context"

我也试过按照这篇文章的建议传递整个容器,但都不起作用。有什么建议吗?

更换

/**
 * @DIInjectParams({"notifications" = @DIInject("pro.notifications")})
 */

带有

/**
 * @DIInjectParams( {
 *     "notifications" = @DIInject("pro.notifications"),
 *     "securityContext" = @DIInject("security.context")
 * } )
 */

我认为DIExtraBundle试图根据参数的名称来猜测服务名称。必须改为显式指定服务id。

如果你的安全上下文依赖于条令,你仍然可以像其他帖子中所说的那样注入容器,例如:

/**
 * @DIService("pro.proposals")
 */
class Test {
    /**
     * @var SecurityContextInterface
     */
    protected $securityContext;
    /**
     * @DIInjectParams({"container" = @DIInject("service_container")})
     */
    function __construct( ContainerInterface $container ) {
        $this->securityContext = $container->get( 'security.context' );
    }
} 

最新更新