I try some basic stuff in Symfony 3.1.5 on my own UserBundle.
First of all, I had a listener that worked. When I create a User, I use it to hash my password and change some stuff.
I declare it in the 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 }
And the listener look like this:
<?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()));
}
}
Now, I want to send a confirmation Email after creating a user. I tryed doing another listener but iy didn't worked.
So I forget my listener, and turned it into a subscriber. It gaves me errors about the arguments and typeint in my method.
Here my User entity:
<?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
{
...
my Service.yml :
user.subscriber.signin:
class: Willuna\UserBundle\Subscriber\SigninSubscriber
arguments: [ '@security.password_encoder' , '@doctrine' ]
tags:
- { name: doctrine.event_subscriber}
The beautiful 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
}
}
I've change my code to look like the documentation of subscribers
Finally, I still have this error when I create my new user :
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
If I change the order of args, it will say the same error about Registry.
If I erase all args, it will say the same error about the args in my method prePersist(LifecycleEventArgs $args)
.
So here are the questions:
Is it possible to create a subscriber with lifecycle event and @doctrine and @encoder in args ?
Am I blind?
Did I miss something?
PS : First time I ask for help in a forum \o/.... except for my hemoroid problems. Just kidding XD.
Sorry for the long post. Here is a potatoe