![](/img/trans.png)
[英]App\Entity\PlaylistForCompany object not found by the @ParamConverter annotation
[英]"App\Entity\User object not found by the @ParamConverter annotation"
當我嘗試訪問其中一條路線時出現問題,出現此錯誤:“@ParamConverter 注釋未找到 App\Entity\CompanyUser object”
很多人都有同樣的問題,但我能看到的解決方案都不能解決我的問題。
我的 function 編輯:
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(),
]);
}
謝謝你的幫助 !
您可以像這樣覆蓋 ParamConverter
/**
* @Route("/{id}/edit", name="company_user_edit", methods={"GET","POST"}, requirements={"id"="\d+"})
* @ParamConverter("id", class="CompanyUser", options={"id": "id"})
*/
路由的舊 {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+"})
就我而言,問題在於整個控制器的路由器名稱錯誤。 我使用了自動執行此操作的生成器。 文件:
/**
* @Route("/document")
*/
class DocumentController extends AbstractController
/**
* @Route("/document/group")
*/
class DocumentGroupController extends AbstractController
我更改了路由器的名稱
@Route("/document/group")
到
@Route("/document_group")
我有獲取用戶個人資料的路線
manage-users:
path: /Account/{id}/Manage
controller: App\Controller\RegistrationController::manage
當請求隨機用戶 ID 時,會拋出Object 未找到錯誤。 我添加了用戶 object 參數默認值為 null,並在 controller 方法中包含了 null 檢查。
public function manage(User $user = null) <= Default parameter value
{
try {
if(null === $user) {
$this->addFlash("error", "User Not Found");
return $this->redirectToRoute("home");
}
...
...
希望這會有所幫助,如果有人正在檢查此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.