Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.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

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
Php Symfony将具有lifecycleevent的侦听器转换为订户_Php_Symfony_Events_Doctrine_Subscriber - Fatal编程技术网

Php Symfony将具有lifecycleevent的侦听器转换为订户

Php Symfony将具有lifecycleevent的侦听器转换为订户,php,symfony,events,doctrine,subscriber,Php,Symfony,Events,Doctrine,Subscriber,我在自己的UserBundle上尝试了symfony3.1.5中的一些基本内容 首先,我有一个倾听者。当我创建一个用户时,我使用它来散列我的密码并更改一些内容 我在服务中声明。yml: #listener user pass et role user.listener.user: class: Willuna\UserBundle\Listener\UserSigninListener arguments: [ '@doctrine', '@security.password

我在自己的UserBundle上尝试了symfony3.1.5中的一些基本内容

首先,我有一个倾听者。当我创建一个用户时,我使用它来散列我的密码并更改一些内容

我在服务中声明。yml:

 #listener user pass et role
 user.listener.user:
    class: Willuna\UserBundle\Listener\UserSigninListener
    arguments: [ '@doctrine', '@security.password_encoder' ]
    tags:
        - { name: doctrine.orm.entity_listener }
听众看起来是这样的:

<?php
namespace Willuna\UserBundle\Listener;

use Willuna\UserBundle\Entity\User;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;
/**
 * Description of UserSigninListener
 *
 */
class UserSigninListener {

private $doctrine;
private $password;

public function __construct(Registry $doctrine, UserPasswordEncoder $password) {
    $this->doctrine = $doctrine;
    $this->password = $password;
}

public function prePersist(User $user, LifecycleEventArgs $args) {
    // I hash and do some stuff in user
    $user->setPassword($this->password->encodePassword($user, $user->getPassword()));

}
}
美丽的订户:

<?php
namespace Willuna\UserBundle\Subscriber;

use Willuna\UserBundle\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;
use Doctrine\Common\EventSubscriber; 
//use Symfony\Component\EventDispatcher\EventSubscriberInterface;

use Doctrine\ORM\Event\LifecycleEventArgs;
//use Doctrine\Common\Persistence\Event\LifecycleEventArgs;

/**
 * class SigninSubscriber
 *
 */
class SigninSubscriber implements EventSubscriber {
    private $password;
    private $doctrine;

    public function __construct(UserPasswordEncoder $password, Registry $doctrine) {
        $this->password = $password;
        $this->doctrine= $doctrine;
    }


    public function getSubscribedEvents()
    {
        return array(
            'prePersist',
            'postPersist'
        );
    }

    public function prePersist(User $user, LifecycleEventArgs $args ){
        //change password and change my user

    }

    public function postPersist(LifecycleEventArgs $args){
        //then I want to send an email 
    }
}
如果我更改args的顺序,它将显示关于注册表的相同错误

如果我删除所有的arg,它将在我的方法
prePersist(LifecycleEventArgs$args)
中对args显示相同的错误

以下是问题:

是否可以在args中创建具有生命周期事件和@doctor和@encoder的订阅服务器

我瞎了吗

我错过什么了吗

PS:我第一次在论坛上寻求帮助\o/。。。。除了我的血液病。只是开玩笑

抱歉发了这么长的邮件。

我自己试过,没有问题。。。您是否尝试从依赖项中删除
@doctrine
(从不在条令侦听器中插入条令或实体管理器)?您好!谢谢你试图救我:)我尝试删除
@doctrine
,然后删除
@security.password\u encoder
,然后问题就出现在我的prePersist(LifecycleEventArgs$args)上。它说我的$args是User的一个实例<代码>类型错误:传递给Willuna\UserBundle\Subscriber\SignInsSubscriber::prePersist()的参数1必须是Doctrine\ORM\Event\LifecycleEventArgs的实例,Willuna\UserBundle\Entity\User的实例给定查看prePersist方法,您正在请求它:
公共函数prePersist(User$User,LifecycleEventArgs$args)
。此方法签名必须是
public function prePersist(LifecycleEventArgs$args)
    user.subscriber.signin:
        class: Willuna\UserBundle\Subscriber\SigninSubscriber
        arguments: [ '@security.password_encoder' , '@doctrine' ]
        tags:
            - { name: doctrine.event_subscriber} 
<?php
namespace Willuna\UserBundle\Subscriber;

use Willuna\UserBundle\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;
use Doctrine\Common\EventSubscriber; 
//use Symfony\Component\EventDispatcher\EventSubscriberInterface;

use Doctrine\ORM\Event\LifecycleEventArgs;
//use Doctrine\Common\Persistence\Event\LifecycleEventArgs;

/**
 * class SigninSubscriber
 *
 */
class SigninSubscriber implements EventSubscriber {
    private $password;
    private $doctrine;

    public function __construct(UserPasswordEncoder $password, Registry $doctrine) {
        $this->password = $password;
        $this->doctrine= $doctrine;
    }


    public function getSubscribedEvents()
    {
        return array(
            'prePersist',
            'postPersist'
        );
    }

    public function prePersist(User $user, LifecycleEventArgs $args ){
        //change password and change my user

    }

    public function postPersist(LifecycleEventArgs $args){
        //then I want to send an email 
    }
}
Type error: Argument 1 passed to Willuna\UserBundle\Subscriber\SigninSubscriber::__construct() 
must be an instance of Symfony\Component\Security\Core\Encoder\UserPasswordEncoder, 
none given, called in /var/www/cours/SYMFONY/willuna-dolls/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/DefaultEntityListenerResolver.php on line 73