“ @ParamConverter批注未找到App \ Entity \ User对象”

时间:2019-04-04 12:37:54

标签: php symfony routing

当我尝试访问它时,我的一条路线有问题,出现以下错误:“ @ ParamConverter批注找不到App \ Entity \ CompanyUser对象”

许多人都有相同的问题,但是我看不到的解决方案都能解决我的问题。

我的功能编辑:

private $passwordEncoder;

public function __construct(UserPasswordEncoderInterface $passwordEncoder)
{
    $this->passwordEncoder = $passwordEncoder;
}

/**
 * @Route("/{id}/edit", name="company_user_edit", methods={"GET","POST"}, requirements={"id"="\d+"})
 */
public function edit(Request $request, UserPasswordEncoderInterface $passwordEncoder, CompanyUser $user): Response
{
    $em = $this->getDoctrine()->getManager();
    $user = $this->getUser();
    $reset_password_form = $this->get('form.factory')->create(CompanyResetPasswordType::class, $user);
    $reset_password_form->handleRequest($request);

    if ($reset_password_form->isSubmitted() && $reset_password_form->isValid()) {

        $this->passwordEncoder->isPasswordValid($this->getUser(), $request->get('password'));
        $oldPassword = $request->request->get('reset_password')['oldPassword'];

        // Si l'ancien mot de passe est bon
        if ($passwordEncoder->isPasswordValid($user, $oldPassword)) {
            $newEncodedPassword = $passwordEncoder->encodePassword($user, $user->getPlainPassword());
            $user->setPassword($newEncodedPassword);

            $em->persist($user);
            $em->flush();

            $this->addFlash('notice', 'Succeed to change your password');

            return $this->redirectToRoute('company_show', ['id' => $user->getCompany()->getId()]);

        } else {
            $reset_password_form->addError(new FormError('Old password is not valid'));
        }
    }

    $edit_form = $this->createForm(CompanyUserType::class, $user);
    $edit_form->handleRequest($request);

    if ($edit_form->isSubmitted() && $edit_form->isValid()) {
        $this->getDoctrine()->getManager()->flush();

        return $this->redirectToRoute('company_show', ['id' => $user->getCompany()->getId()]);
    }

    return $this->render('user/edit.html.twig', [
        'user' => $user,
        'edit_form' => $edit_form->createView(),
        'reset_password_form' => $reset_password_form->createView(),
    ]);
}

感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

您可以像这样覆盖ParamConverter

-bash: domainname: command not found

答案 1 :(得分:0)

路由的旧{id}部分必须更改为与您要转换的参数相同的名称。

即更改:

 * @Route("/{id}/edit", name="company_user_edit", methods={"GET","POST"}, requirements={"id"="\d+"})

收件人

 * @Route("/{user}/edit", name="company_user_edit", methods={"GET","POST"}, requirements={"id"="\d+"})

答案 2 :(得分:0)

就我而言,问题在于整个控制器的路由器名称错误。我使用了自动执行此操作的生成器。 文件:

/**
 * @Route("/document")
 */
class DocumentController extends AbstractController


/**
 * @Route("/document/group")
 */
class DocumentGroupController extends AbstractController

我更改了路由器的名称

@Route("/document/group") 

@Route("/document_group")