Symfony将带有lifecycleevent的监听器转变为用户

时间:2017-03-11 14:57:02

标签: php symfony events doctrine subscriber

我在自己的UserBundle上尝试了Symfony 3.1.5中的一些基本内容。

首先,我有一个有效的倾听者。当我创建一个用户时,我用它来哈希我的密码并改变一些东西。

我在service.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()));

}
}

现在,我想在创建用户后发送确认电子邮件。 我尝试做另一个听众,但我没有工作。

所以我忘了我的听众,把它变成了一个订阅者。它给我带来了关于我的方法中的参数和typeint的错误。

这是我的用户实体:

<?php
namespace Willuna\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity(repositoryClass="Willuna\UserBundle\Repository\UserRepository")
 * @UniqueEntity(fields="email", message="Email already taken")
 * @UniqueEntity(fields="username", message="Username already taken")
 * @ORM\EntityListeners({"Willuna\UserBundle\Subscriber\SigninSubscriber"})
 * @ORM\HasLifecycleCallbacks()
 */
class User implements UserInterface, \Serializable
{
    ...

我的Service.yml:

    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 
    }
}

我已将代码更改为the documentation of subscribers

最后,我在创建新用户时仍然遇到此错误:

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 

如果我改变了args的顺序,它会说出与Registry相同的错误。

如果我删除所有args,它会在方法prePersist(LifecycleEventArgs $args)中对args说同样的错误。

以下是问题:

是否可以在args中创建具有生命周期事件和@doctrine和@encoder的订户?

我是瞎了吗?

我错过了什么吗?

PS:我第一次在论坛上寻求帮助\ o / ....除了我的类风湿性问题。开个玩笑XD。

对不起,很长的帖子。 Here is a potatoe

0 个答案:

没有答案