2017-05-07 62 views
1

我在同一個控制器中有兩個動作。 我發現從第一個動作傳遞到第二個動作的問題。 我試圖使用正向方法http://symfony.com/doc/current/controller/forwarding.html在symfony中的同一個控制器中傳遞從一個動作發佈到另一個動作的值

順便說一句這兩個動作('轉發'和'轉發到')都有提交表單。

問題是我無法在第二個動作中訪問變量「$ param1」。它總是變爲空。

有什麼我在這裏錯過了嗎?

這裏是我的代碼:

這是第一個動作:

/** 
    * 
    * @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(),)); 
    } 

這是第二招:

/** 
    * 
    * @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(),)); 
    } 
+0

你爲什麼要這樣做?在Symfony中轉發並不是一個好的做法。 – COil

+0

我沒有找到其他解決方案,所以我嘗試了正向方法。你有什麼想法,我怎麼能做不同? – User

回答

2

首先,正向只需要低谷不同的控制器中,相同的控制器,而不是使前鋒簡單地調用其他動作:

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

另一方面,基於你的方法是沒有必要向前或調用check2Action。

這是根據你的榜樣我的建議,(非測試代碼)

/** 
* 
* @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(),)); 
} 

措施2:

/** 
    * 
    * @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(),)); 
    } 

在動作1提交上面例子中的IP作爲參數CHECK2傳遞重定向,那麼提交的動作2將與該IP一起被使用,並且始終可用。

+0

非常感謝@rafrsr!它工作:))但如何將參數從redirectToRoute傳遞到樹枝路徑?我試過這個Re-check,我得到了這個錯誤「變量」ip「在tiwg中不存在」 – User

+0

你的例子是正確的,但需要通過ip參數來分支,例如,如果你需要在** host/new2中顯示ip .html.twig **在渲染參數中添加''ip'=> $ ip'。 – rafrsr

+0

如果你有任何想法和提前致謝,你能幫助我嗎?)@rafrsr http://stackoverflow.com/questions/43891514/draw-charts-with-a-loop-using-highcharts-in -symfony – User

相關問題