Redirect to the route in Symfony

Viewed 41

I'm building an app with Symfony, and I wonder how to redirect the newly created User(partner) to the route which belong to this User(partner), because I need the id of this same User(partner) to create another entity and to not return null for the OneToOne relation.Thank you for your help.

Controller.php [EDITED]:

#[Route('/new', name: 'app_partenaire_new', methods: ['GET', 'POST'])]
public function new(Request $request, PartenaireRepository $partenaireRepository, UserPasswordHasherInterface $userPasswordHasher): Response
{
    $partenaire = new Partenaire();
    $form = $this->createForm(PartenaireType::class, $partenaire);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $partenaire->setIsActive(true);
        $partenaire->setRoles(['ROLE_PARTENAIRE']);
        $partenaire->setPassword(
            $userPasswordHasher->hashPassword(
                $partenaire,
                $form->get('plainPassword')->getData()
            ));
        $partenaireRepository->add($partenaire, true);

        return $this->redirectToRoute('app_partenaire_permission', ['id'=>$partenaire->getId()], Response::HTTP_SEE_OTHER); 
    }

    return $this->renderForm('partenaire/new.html.twig', [
        'partenaire' => $partenaire,
        'form' => $form,
    ]);
}

#[Route('/{id}/permission', name: 'app_partenaire_permission', methods: ['GET', 'POST'])] // id of the newly created post /permission
public function permission(Request $request, EntityManagerInterface $entityManager): Response
{
    $partenairePermission = new PartenairePermission();
    $form = $this->createForm(PartenairePermissionType::class, $partenairePermission);
    $form->handleRequest($request);
    $partenaire = $entityManager->getRepository(Partenaire::class)->findOneBy([ // get the newly created partenaire id 
        'id' => $request->get('id')
    ]);


    if ($form->isSubmitted() && $form->isValid()) {
        $partenairePermission->setPartenaire($partenaire);
        $entityManager->persist($partenairePermission);
        $entityManager->flush();
        return $this->redirectToRoute('app_partenaire_index', [], Response::HTTP_SEE_OTHER);
    }
    return $this->render('partenaire/permission.html.twig', [
        'partenaire' => $partenaire,
        'partenairePermission' => $partenairePermission,
        'form' => $form->createView(),
    ]);
}
0 Answers
Related