1

I have 2 actions in the same controller. I find problem in passing posted values from the 1st action to the the 2nd one. I tried to use forward method http://symfony.com/doc/current/controller/forwarding.html

BTW both actions (the 'forwarding' and the 'forwarded to') have forms with submissions.

Problem is I couldn't access the variable " $param1 " in the second action. It always goes null.

Is there anything I have missed here?

Here is my code:

This is the 1st action:

    /**
     *
     * @Route("/new1", name="command_check1")
     */
    public function check1Action(Request $request)
    {
        $host = new Host();
        $form = $this->createFormBuilder($host)
            ->add("iPaddress", TextType::class)
            ->getForm();

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid())
         {
             $a= $form["iPaddress"]->getData(); //$a= '127.0.0.1'

          **$this->forward('AcmeBundle:Command:check2', array('param1' => $a));**

           if($this->pingAction($a)==true){
             return $this->redirectToRoute('command_check2'); }

         }

          return $this->render('host/new1.html.twig', array(
            'host' => $host,
            'form' => $form->createView(),));
    }

This is the 2nd action:

    /**
     *
     * @Route("/new2", name="command_check2")
     */
    public function check2Action(Request $request, **$param1**)
    {

   **var_dump($param1); // here i can get the posted value $param1= '127.0.0.1'**

        $host = new Host();
        $form = $this->createFormBuilder($host)
            ->add("login", TextType::class)
            ->add("password", TextType::class) 
            ->getForm();
        $form->handleRequest($request);

    **var_dump($param1); // until here it works good $param1= '127.0.0.1'**


        if ($form->isSubmitted() && $form->isValid()) 
        {

    **// the problem is here after submitting the 2nd form
            var_dump($param1); // $param= null**


             $b= $form["login"]->getData();
             $c= $form["password"]->getData();

        }

        return $this->render('host/new2.html.twig', array(
            'host' => $host,
            'form' => $form->createView(),));
    }
User
  • 51
  • 2
  • 7
  • Why do you want to do this ? Forwarding is not a good practice in Symfony. – COil May 07 '17 at 15:46
  • I didn't found another solution so i tried the forward method. Do you have any idea how can I do it differently? – User May 07 '17 at 15:53

1 Answers1

2

Firstly, Forward is only necessary trough different controllers, in the same controller instead of make a forward simply call the other action:

$this->check2Action($request, $a);

In the other hand based on your approach is nos necessary make a forward or call to the check2Action.

This is my recommendation based on your example, (non tested code)

/**
 *
 * @Route("/new1", name="command_check1")
 */
public function check1Action(Request $request)
{
    $host = new Host();
    $form = $this->createFormBuilder($host)
        ->add("iPaddress", TextType::class)
        ->getForm();

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid())
     {
         $a = $form["iPaddress"]->getData(); //$a= '127.0.0.1'

       if($this->pingAction($a)==true){
         return $this->redirectToRoute('command_check2', ['ip' => $a]);
       }
      }

      return $this->render('host/new1.html.twig', array(
        'host' => $host,
        'form' => $form->createView(),));
}

Action2:

   /**
     *
     * @Route("/new2/{ip}", name="command_check2")
     */
    public function check2Action(Request $request, $ip)
    {

        $host = new Host();
        $form = $this->createFormBuilder($host)
            ->add("login", TextType::class)
            ->add("password", TextType::class) 
            ->getForm();
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) 
        {

            var_dump($ip); 

             $b= $form["login"]->getData();
             $c= $form["password"]->getData();

        }

        return $this->render('host/new2.html.twig', array(
            'host' => $host,
            'form' => $form->createView(),));
    }

In the above example the IP submitted in the action1 is passed as argument to check2 redirect, then the submission of action2 is taken with this IP and is always available.

rafrsr
  • 1,940
  • 3
  • 15
  • 31
  • Thanks a Lot @rafrsr !! It works :)) but how to pass the parameters from the redirectToRoute to the twig path ? I tried this Re-check and i got this error "Variable "ip" does not exist in tiwg" – User May 08 '17 at 09:46
  • your example is correct, but is needed pass the ip parameter to twig, for example if you need show the ip in **host/new2.html.twig** add `'ip'=> $ip` in render parameters. – rafrsr May 08 '17 at 12:53
  • Can you help me with this please if you have any idea and thanks in advance :)) @rafrsr http://stackoverflow.com/questions/43891514/draw-charts-with-a-loop-using-highcharts-in-symfony – User May 10 '17 at 13:33