On my Symfony 3 project I have made the following contoller:
/**
* @Route("/person_in_need/{id}",name="person_in_need_view")
*/
public function viewPersonInNeed(Request $request,$id)
{
}
And I want to redirect in it from another controller eg:
/**
* @Route("/person_in_need/add",name="add_person_in_need")
*/
public function addPersonInNeed(Request $request)
{
$form=$this->createForm(PersonInNeedType::class,new PersonInNeed());
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/**
* @var PersonInNeedBusinessLogicModel $dataInsert
*/
$dataInsert=$this->get('app.person_in_need_business_model');
$name=$form->get(PersonInNeedConstats::PERSON_IN_NEED_NAME)->getData();
$surname=$form->get(PersonInNeedConstats::PERSON_IN_NEED_SURNAME)->getData();
$reason=$form->get(PersonInNeedConstats::PERSON_IN_NEED_REASON)->getData();
$dataInsert->registerPersonInNeed($name,$surname,$reason);
//Pass id there
// return $this->redirectToRoute('person_in_need_view');
}
return $this->render('social_worker/add_person_in_need.html.twig',array(
'form'=>$form->createView()
));
}
How I will redirect and pass the id
parameter using the route name?
Actualy you're almost done ;) TL;DR just use
return $this->redirectToRoute('person_in_need_view', ['id' => $yourEntity->getId()]);
I'm a bit curious why you get data via $form->get('<FIELD_NAME>)->normData()
but there're posibly a reson for that...
I hope you've configured your PersonInNeedType
for your PersonInNeed
Entity. If so, just call
if ($form->isSubmitted() && $form->isValid()) {
/** @var PersonInNeed personInNeed **/
$personInNeed = $form->getData();
//at the end...after persist and flush so your entity is stored in DB and you have an ID
return $this->redirectToRoute('person_in_need_view', ['id' => $personInNeed->getId()]);
}