Leo
Leo

Reputation: 2103

How to access POST parameters

I'm trying to learn symfony PHP framework but unlike Ruby on Rails it is not really intuitive.

I'm rendering a form with newAction that looks like this

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


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

it renders very simple form:

<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" />

That points to the 'create' action:

public function createAction()
        {
            $product = new Product();
            $product->setName('A Foo Bar');
            $product->setPrice('19.99');
            $product->setDescription('Lorem ipsum dolor');

            $em = $this->getDoctrine()->getManager();
            $em->persist($product);
            $em->flush();


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

Right now this action only saves the same static values, but i would like it to access data from the form above. My question is very easy (i hope). How can i save this previously sent informations ie how can i get POST parameters inside createAction?

Upvotes: 2

Views: 148

Answers (3)

Serge Kvashnin
Serge Kvashnin

Reputation: 4491

If I correctly understood you:

// ...
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()
                ));
}

And here is the link to doc page. If you need directly get some request parameter you can use $request->request->get('your_parameter_name'); for POST and $request->query->get('your_parameter_name'); for GET.

Also you can get your form data like: $form->get('your_parameter_name')->getData();.

By the way, as mentioned above, you don't need two actions.

Upvotes: 0

Nico
Nico

Reputation: 3569

Something that can help you :

http://www.leaseweblabs.com/2013/04/symfony2-crud-generator/

Very useful short videos that explain how to use CRUD generator command lines. And then if you use CRUD generator which is in Symfony2 by default, you will have your action newAction self created and you'll be able to read code generated (best practice code with your symfony version) and try to understand it.

Upvotes: 1

Yassine EL MALKI
Yassine EL MALKI

Reputation: 162

No need for two actions, you can add this in the same action 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()
                        ));
}

Upvotes: 2

Related Questions