Php 如何对密码进行编码?

Php 如何对密码进行编码?,php,symfony,Php,Symfony,我对symfony2很陌生,所以这可能是一个有点蹩脚的问题;-) 我有一个表格,可以编辑两个实体-工作者和用户。两者都是相关的。用户用于登录,worker存储有关用户的附加信息(在系统中,我有3种不同类型的用户:worker、client和admin)。下面是生成的updateAction,我只想在保存已编辑的用户之前对密码进行编码。怎么做 public function updateAction(Request $request, $id) { $em = $this->getD

我对symfony2很陌生,所以这可能是一个有点蹩脚的问题;-) 我有一个表格,可以编辑两个实体-工作者和用户。两者都是相关的。用户用于登录,worker存储有关用户的附加信息(在系统中,我有3种不同类型的用户:worker、client和admin)。下面是生成的updateAction,我只想在保存已编辑的用户之前对密码进行编码。怎么做

public function updateAction(Request $request, $id)
{
    $em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('MainBundle:Worker')->find($id);

if (!$entity) {
    throw $this->createNotFoundException('Unable to find Worker entity.');
}

$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);

if ($editForm->isValid()) {

    $em->flush();

    return $this->redirect($this->generateUrl('worker_edit', array('id' => $id)));
}

return $this->render('MainBundle:Worker:edit.html.twig', array(
    'entity'      => $entity,
    'edit_form'   => $editForm->createView(),
    'delete_form' => $deleteForm->createView(),
));

}

查看有关密码编码的文档

基本上,您将调用安全工厂,获取要为其编码的用户,获取编码器,然后对密码进行编码

$factory = $this->get('security.encoder_factory');
$user = //get the user from the worker entity or just use the worker entity


$encoder = $factory->getEncoder($user);
$theSuppliedPassword = // however they are supplying the password, likely $entity->getPassword();
$password = $encoder->encodePassword($theSuppliedPassword, $user->getSalt());
$user->setPassword($password);