【问题标题】:Symfony2: Sending arbitrary POST data from one page to the nextSymfony2:将任意 POST 数据从一页发送到下一页
【发布时间】:2015-07-18 20:36:43
【问题描述】:

我的控制器刚刚处理完一个表单,并且

if ($form->isValid())
{
  // Persist my data.
  // ...

  // Now move on to the next page.
  return $this->redirectToRoute('page2');
}

但我想使用 POST 将变量传递给该页面。 IE。类似于:

$this->redirectToRoute('page2', array('state' => 'wonderful'));

这显然会将它作为 GET 参数传递。人们建议使用redirect 307s and forwarding,但他们只是转发当前请求。我需要做的是使用我的任意消息创建一个新请求。我怎样才能做到这一点?

【问题讨论】:

    标签: php symfony post controller


    【解决方案1】:

    不确定这是否可行,但值得一试:

    // Add here whatever you need to the request
    $request->initialize(
       $request->query->all(),
       $request->request->all(),
       $request->attributes->all(),
       $request->cookies->all(),
       $request->files->all(),
       $request->server->all(),
       $request->getContent());
    
    return $this->redirectToRoute('route', [
            'request' => $request
        ], 307);
    

    【讨论】:

      【解决方案2】:

      将您的重定向委托给客户端并在那里发出发布请求。

      你的控制器:

      if ($form->isValid())
      {
        return $this->render('redirect.html.twig');
      }
      

      redirect.html.twig:

      <html>
      <body onload="document.frm1.submit()">
          <form action="{{path('page2')}}" method="POST" name="frm1">
              <input type="hidden" name="state" value="wonderful" />
          </form>
      </body>
      </html>
      

      顺便说一句,您可以将 post 参数绑定到 Symfony 表单并在模板中渲染它。

      【讨论】:

      • JavaScript 似乎不是一个特别干净的解决方案。不过还是谢谢你。
      【解决方案3】:

      您可以使用session

      在初始控制器中

      $this->get('session')->set('request',$request)
      

      在其他

      $request=$this->get('session')->get('request')
      

      或者你可以使用cookies

      $response->headers->setCookie(new Cookie('request', $request));
      

      ->

      $response->headers->getCookie('request'));
      

      【讨论】:

        【解决方案4】:

        您不能直接模拟 POST。
        如果您必须使用 POST 发送,则必须使用:

        检查或询问您的表格。
        我觉得必须在项目的另一条路由中发送 POST 数据很奇怪。

        【讨论】:

          猜你喜欢
          • 2012-09-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-01-30
          • 2020-08-16
          • 1970-01-01
          相关资源
          最近更新 更多