在symfony的同一控制器中将发布的值从一个动作传递到另一个动作
Passing posted values from one action to another in the same controller in symfony
我在同一个控制器中有 2 个动作。
我发现将发布的值从第一个操作传递到第二个操作时出现问题。
我尝试使用转发方法 http://symfony.com/doc/current/controller/forwarding.html
顺便说一句,这两个操作('forwarding' 和 'forwarded to')都有提交表格。
问题是我无法在第二个操作中访问变量“$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(),));
}
首先,转发只需要通过不同的控制器,在同一个控制器中而不是转发只需调用另一个动作:
$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(),));
}
在上面的示例中,action1 中提交的 IP 作为参数传递给 check2 重定向,然后 action2 的提交使用此 IP 并始终可用。
我在同一个控制器中有 2 个动作。 我发现将发布的值从第一个操作传递到第二个操作时出现问题。 我尝试使用转发方法 http://symfony.com/doc/current/controller/forwarding.html
顺便说一句,这两个操作('forwarding' 和 'forwarded to')都有提交表格。
问题是我无法在第二个操作中访问变量“$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(),));
}
首先,转发只需要通过不同的控制器,在同一个控制器中而不是转发只需调用另一个动作:
$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(),));
}
在上面的示例中,action1 中提交的 IP 作为参数传递给 check2 重定向,然后 action2 的提交使用此 IP 并始终可用。