Php 如何访问POST参数

Php 如何访问POST参数,php,symfony,Php,Symfony,我试图学习SymfonyPHP框架,但与RubyonRails不同,它并不是真正直观的 我正在用newAction呈现一个表单,如下所示 public function newAction() { $product = new Product(); $form = $this->createForm(new ProductType(), $product); return $t

我试图学习SymfonyPHP框架,但与RubyonRails不同,它并不是真正直观的

我正在用newAction呈现一个表单,如下所示

public function newAction()
        {   
            $product = new Product();
            $form = $this->createForm(new ProductType(), $product);


                    return $this->render('AcmeStoreBundle:Default:new.html.twig', array(
                            'form' => $form->createView()
                        ));
        }
它呈现了非常简单的形式:

<form action="{{ path("AcmeStoreBundle_default_create") }}" method="post" {{ form_enctype(form) }} class="blogger">
        {{ form_errors(form) }}

        {{ form_row(form.price) }}
        {{ form_row(form.name) }}
        {{ form_row(form.description) }}

        {{ form_rest(form) }}

        <input type="submit" value="Submit" />

现在这个操作只保存相同的静态值,但我希望它从上面的表单访问数据。我的问题很简单(我希望如此)。如何保存以前发送的信息,即如何在createAction中获取POST参数?

不需要两个操作,您可以在同一个操作中添加此操作newAction

public function newAction()
        {   
            $product = new Product();
            $form = $this->createForm(new ProductType(), $product);
            $em = $this->getDoctrine()->getManager();

        $request =  $this->getRequest();

        if($request->getMethod() == 'POST'){
            $form->bindRequest($request);/*or  $form->handleRequest($request); depends on symfony version */
            if ($form->isValid()) {
              $em->persist($product);
            $em->flush();
       return $this->redirect($this->generateUrl('some_route'),array(params));
           }
       }
  return $this->render('AcmeStoreBundle:Default:new.html.twig', array(
                            'form' => $form->createView()
                        ));
}

如果我正确理解了你:

// ...
use Symfony\Component\HttpFoundation\Request;
// ...

public function createAction(Request $request)
{
    $product = new Product();
    $form = $this->createForm(new ProductType(), $product);

    $form->handleRequest($request);

    if($form->isValid())
    {
        $em = $this->getDoctrine()->getManager();
        $em->persist($product);
        $em->flush();

        return $this->redirect("/show/{$product->getId()}");
    }


    return $this->render('AcmeStoreBundle:Default:new.html.twig', array(
                    'form' => $form->createView()
                ));
}
这是ToDoc页面。 如果您需要直接获取一些请求参数,您可以使用
$request->request->get('your_parameter_name')用于
POST
$request->query->get('your_参数_名称')用于
获取

您还可以获取表单数据,如:
$form->get('your_参数_name')->getData()


顺便说一句,如上所述,你不需要两个动作。

一些可以帮助你的东西:


介绍如何使用CRUD生成器命令行的非常有用的短视频。然后,如果您使用默认情况下在Symfony2中的CRUD生成器,您将拥有自己创建的action newAction,您将能够阅读生成的代码(使用symfony版本的最佳实践代码)并尝试理解它。

好的,thx,但如果我需要使用两个action怎么办,你能告诉我怎么做吗?最好的方法是用同样的动作来做,这就是我用来解决问题的方法。我编辑了我的答案以符合您的要求。好的,但请告诉我为什么不定义$em?这里是什么?就我所知,您只需对其调用persist()方法$em=$this->getDoctrine()->getManager();注意:
$this->getRequest()
将在未来版本的Symfony2中删除。尽量不要再使用它了。我相信,将其划分为两个独立的操作会使控制器中的一切更加优雅,逻辑更少(如果method==post)better@Leo您不需要检查该方法是否为POST。看医生。
// ...
use Symfony\Component\HttpFoundation\Request;
// ...

public function createAction(Request $request)
{
    $product = new Product();
    $form = $this->createForm(new ProductType(), $product);

    $form->handleRequest($request);

    if($form->isValid())
    {
        $em = $this->getDoctrine()->getManager();
        $em->persist($product);
        $em->flush();

        return $this->redirect("/show/{$product->getId()}");
    }


    return $this->render('AcmeStoreBundle:Default:new.html.twig', array(
                    'form' => $form->createView()
                ));
}