Hello, everyone.
I am building symphony 5 project, but I have an issue.
The URL is not redirecting after I create or edit an element.
But in other functions, the redirectToRoute() function works.
Strange problem…
These are my controller code.
Please check it and let me know what I missed.
Thanks.
/**
* @Route("/list", name="animals")
*/
public function animals(): Response
{
if (!$this->getUser()) {
$this->redirectToRoute('app_login');
}
$animals = $this->getUser()->getAnimals();
return $this->render('animals/overview.html.twig', [
'animals' => $animals,
]);
}
And
/**
* @Route("/create", name="animal_create")
*/
public function create(Request $request, EntityManagerInterface $entityManager): Response
{
$animal = new Animal();
$customer = $this->em()->getRepository(Customer::class)->findOneBy(array('id' => $request->request->get('customer')));
$animal->setCustomer($customer);
$form = $this->createForm(AnimalType::class, $animal, [
'csrf_protection' => false,
]);
$form->handleRequest($request);
if (!$form->isSubmitted() || !$form->isValid()) {
return new Response('failed');
}
$customer->addAnimal($animal);
$entityManager->persist($animal);
$entityManager->flush();
return $this->redirectToRoute('animals');
}
/**
* @Route("/{id}/update", name="animal_update")
*/
public function update(Request $request, Animal $animal, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(AnimalType::class, $animal, [
'csrf_protection' => false,
]);
$form->handleRequest($request);
if (!$form->isSubmitted() || !$form->isValid()) {
return new Repsonse('failed');
}
$entityManager->persist($animal);
$entityManager->flush();
return $this->redirectToRoute('animals');
}