为什么控制器执行两次(flashbag, redirectoroute)


Why is the controller executed twice (flashbag, redirectoroute)?

我在yml中配置了两个路由/alpha和/beta。在alphaAction中,一个通知被放置在flashbag中,并发生一个redirecttoroute。在betaAction中,闪光袋中的通知被阅读。

当我在浏览器中尝试/alpha时,有时会收到两个通知,有时会收到一个通知。

谁能解释一下发生了什么,我做错了什么

public function alphaAction()
{
  $this->get('session')->getFlashBag()->add("notice",mt_rand());
  return $this->redirectToRoute("beta");
}
public function betaAction()
{
  $notices= $this->get('session')->getFlashBag()->get('notice');    
  return new Response(implode($notices,", "));
}

使用add方法,我可以重现您描述的问题。这可以通过使用set法而不是add(或setFlash)法来解决。

public function alphaAction()
{
  $this->get('session')->getFlashBag()->set("notice",mt_rand());
  return $this->redirectToRoute("beta");
}
public function betaAction()
{
  $notices= $this->get('session')->getFlashBag()->get('notice');    
  return new Response(implode($notices,", "));
}