Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony 在FOSUserBundle事件中获取twilio实例_Symfony_Symfony 2.3 - Fatal编程技术网

Symfony 在FOSUserBundle事件中获取twilio实例

Symfony 在FOSUserBundle事件中获取twilio实例,symfony,symfony-2.3,Symfony,Symfony 2.3,我正试图通过Twilio向在我的网站上注册的用户发送短信,我得到了vresh/Twilio捆绑包,效果很好。 我试图将twilio实例传递给该活动,但我认为我遗漏了一些东西,以下是我正在做的: 在config.yml中,我将服务设置为: services: registration.completed.listener: class: Jaguar\AloBundle\EventListener\RegistrationEventListener argu

我正试图通过Twilio向在我的网站上注册的用户发送短信,我得到了vresh/Twilio捆绑包,效果很好。 我试图将twilio实例传递给该活动,但我认为我遗漏了一些东西,以下是我正在做的:

在config.yml中,我将服务设置为:

services:
    registration.completed.listener:
        class: Jaguar\AloBundle\EventListener\RegistrationEventListener
        arguments:
            entityManager: ["@doctrine.orm.voipswitch_entity_manager", "vresh_twilio"]
        tags:
            - { name: kernel.event_subscriber, event: performOnRegistrationCompleted }
我已经声明了twilio配置:

vresh_twilio:
    sid: 'xxx'
    authToken: 'xxx'
    version: '2010-04-01'
    retryAttempts: 3
然后,在我的方法中,我尝试获取实例:

public function performOnRegistrationCompleted(UserEvent $event) 
{
    $twilio = $event->get('vresh_twilio');
}
但它失败了

请问有什么帮助吗


非常感谢

您的服务设置存在一些问题

  • 您实际上没有传递Twilio实例,因为在服务名称前面没有
    @
    符号
    @vresh_twilio
    是一项服务,
    vresh_twilio
    只是一个字符串

  • 您正在传入一个关联数组,其中包含一个键
    entityManager
    ,以及一个值,该值也是一个数组,该数组的值为服务
    @doctor.orm.voipswitch\u entity\u manager
    和字符串
    vresh\u twilio

  • 您没有在事件中传递Twilio实例,而是在构造函数中使用Twilio实例构建侦听器

  • 你的服务应该看起来像

    services:
        registration.completed.listener:
            class: Jaguar\AloBundle\EventListener\RegistrationEventListener
            arguments:
                entityManager: "@doctrine.orm.voipswitch_entity_manager"
                twilio: "@vresh_twilio"
                // Or
                // - @doctrine.orm.voipswitch_entity_manager
                // - @vresh_twilio
                // Or
                // [@doctrine.orm.voipswitch_entity_manager, @vresh_twilio]
                //
                // As they all mean the same thing and the keys aren't
                // used in your actual service __construct
            tags:
                - { name: kernel.event_subscriber, event: performOnRegistrationCompleted }
    
    这意味着您的侦听器将有一个构造函数来接收这些服务,如

    protected $entityManager;
    protected $twilio;
    
    public function __conctruct(ObjectManager $entityManager, TwilioWrapper $twilio)
    {
        $this->entityManager = $entityManager;
        $this->twilio = $twilio;
    }
    
    这意味着您可以在类中使用
    $this->twilio
    调用它

    此外,从创建的角度来看,您希望注入的服务似乎是
    @twilio.api
    ,而不是
    @vresh_twilio
    ,因为它似乎不存在,但我可能错了(我自己没有使用捆绑包)