我在同一個控制器中有兩個動作。 我發現從第一個動作傳遞到第二個動作的問題。 我試圖使用正向方法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(),));
}
你爲什麼要這樣做?在Symfony中轉發並不是一個好的做法。 – COil
我沒有找到其他解決方案,所以我嘗試了正向方法。你有什麼想法,我怎麼能做不同? – User