Dimitrios Desyllas
Dimitrios Desyllas

Reputation: 10028

Symfony 3 Redirect to another routhe with url parameter

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?

Upvotes: 1

Views: 6145

Answers (2)

V-Light
V-Light

Reputation: 3115

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()]);
}

Upvotes: 4

Kep
Kep

Reputation: 5857

See Link:

This redirects using a 301 return code to the client:

$this->redirect(
    $this->generateUrl(
        'person_in_need_view',
        array('id' => $id)
    )
);

This redirects internally:

$this->forward(
    'person_in_need_view',
    array('id' => $id)
)

Upvotes: 3

Related Questions