Building Symfony 4.1 app. In my ProfileController ...
I have a booking_new method with form to create a new booking:
/**
* @Route("/profile/booking/new", name="profile_booking_new")
*/
public function booking_new(EntityManagerInterface $em, Request $request)
{
$form = $this->createForm(BookingFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var @var Booking $booking */
$booking = $form->getData();
$booking->setUser($this->getUser());
$em->persist($booking);
$em->flush();
return $this->redirectToRoute('profile_booking_show');
}
return $this->render('profile/bookings/booking_new.html.twig',[
'bookingForm' => $form->createView()
]);
}
Then I have a booking_show method to render single booking pages with route set to booking id:
/**
* @Route("/profile/booking/{id}", name="profile_booking_show")
*/
public function booking_show(BookingRepository $bookingRepo, $id)
{
/** @var Booking $booking */
$booking = $bookingRepo->findOneBy(['id' => $id]);
if (!$booking) {
throw $this->createNotFoundException(sprintf('There is no booking for id "%s"', $id));
}
return $this->render('profile/bookings/booking_show.html.twig', [
'booking' => $booking,
]);
}
After the booking is created I want to redirect user to the show booking view with the correct id.
Run server and get this error ...
ERROR: Some mandatory parameters are missing ("id") to generate a URL for route "profile_booking_show".
I understand the error but how do I fix it? How do I set the id of the booking just created without needing to query for the id?
Once the new entity is persited & flushed, you can use it like:
$em->persist($booking);
$em->flush();
return $this->redirectToRoute('profile_booking_show', ['id' => $bookig->getId()]);