在 Eventlister 中,我想获得security.encoder_factory
. 听众UserManager
确实知道容器,所以$this->get('security.encoder_factor')
显然不会工作。我应该如何security.encoder_factory
在这个监听器中获得属性?
服务:
services:
my.listener:
class: Bla\OrmBundle\EventListener\UserManager
tags:
- { name: doctrine.event_listener, event: prePersist }
用户管理器:
class UserManager {
public function prePersist(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if ($entity instanceof User) {
$factory = $this->get('security.encoder_factory');
$encoder = $factory->getEncoder($entity);
$password = $encoder->encodePassword($entity->getPassword(), $entity->getSalt());
$entity->setPassword($password);
}
}
}