我正在尝试为 Sulu CMF 前端进行用户注册。我想对密码进行哈希处理。但出于任何原因,我无法获得该服务。
You have requested a non-existent service "security.encoder_factory".
或者
You have requested a non-existent service "security.password_encoder"
如果我要求
app/console container:debug | grep encode
两种服务都在那里
$ app/console container:debug | grep encode
263: security.encoder_factory Symfony\Component\Security\Core\Encoder\EncoderFactory
266: security.password_encoder Symfony\Component\Security\Core\Encoder\UserPasswordEncoder
你有什么建议给我吗?我的控制器知道容器,我的代码如下:
$this->container->get('security.password_encoder')
下面是 conainer:debug 的一些输出
$ app/console container:debug security.password_encoder
[container] Information for service security.password_encoder
Service Id security.password_encoder
Class Symfony\Component\Security\Core\Encoder\UserPasswordEncoder
Tags -
Scope container
Public yes
Synthetic no
Lazy no
Synchronized no
Abstract no
$ app/console container:debug security.encoder_factory
[container] Information for service security.encoder_factory
Service Id security.encoder_factory
Class Symfony\Component\Security\Core\Encoder\EncoderFactory
Tags -
Scope container
Public yes
Synthetic no
Lazy no
Synchronized no
Abstract no
我目前安装的 symfony 版本是 v2.6.12
sulu 提供的命令使用了这种方法。它适用于开发和生产两种环境。
/**
* Encodes the given password, for the given password, with he given salt and returns the result.
*
* @param $user
* @param $password
* @param $salt
*
* @return mixed
*/
private function encodePassword($user, $password, $salt)
{
/** @var PasswordEncoderInterface $encoder */
$encoder = $this->getContainer()->get('security.encoder_factory')->getEncoder($user);
return $encoder->encodePassword($password, $salt);
}