6

I'm using Symfony 2.3, and I have a method that looks like this:

public function addAction() {

    $user = new User();
    $form = $this->createForm(new UserType(), $user);
    $form->handleRequest($this->getRequest());

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

        try {
            return $this
                    ->render(
                            'SomeBundle:Default:adduser.html.twig',
                            array('id' => $user->getId()));
        } catch (Exception $e) {
            throw new HttpException(500, "Error persisting");
        }
    }

    throw new HttpException(400, "Invalid request");
}

with this route:

some_bundle_adduserpage:
pattern: /user
defaults: { _controller: SomeBundle:User:add }
methods: [POST]

and it works just fine. I also have this method:

public function editAction($id) {

    $response = new Response();

    if (!$this->doesUserExist($id)) {
        $response->setStatusCode(400);
        return $response;
    }

    $user = new User();
    $form = $this->createForm(new UserType(), $user);
    $form->handleRequest($this->getRequest());

    if (!$form->isValid()) {
        $response->setStatusCode(400);
        return $response;
    }

    $user->setId($id);
    $em = $this->getDoctrine()->getManager();

    try {
        $em->persist($user);
        $em->flush();

        $response->setStatusCode(200);
    } catch (Exception $e) {
        $response->setStatusCode(500);
    }

    return $response;
}

with this route:

some_bundle_edituserpage:
pattern: /user/{id}
defaults: { _controller: SomeBundle:User:edit }
methods: [PUT]
requirements:
    id: \d+

and it doesn't work. I can craft some request and POST it just fine, but the PUT doesn't work. Specifically, it looks like I'm not getting any parameters inside of $this->getRequest(). Why does $this->getRequest() seem to work for POST, but not PUT? What am I doing wrong?

bstempi
  • 2,023
  • 1
  • 15
  • 27
  • does the parameters appear in the browser inspector? – Mohammad AbuShady Jan 22 '14 at 06:17
  • Im using the Postman Chrome plugin, so I can see that they're all there. I should be able to use the same form body for both the `POST` and `PUT`. I've confirmed that `POST` works and `PUT` does not. It's also worth nothing that I'm using an actually HTTP PUT and not the `_method` query parameter. – bstempi Jan 22 '14 at 06:20

2 Answers2

1

This did the trick:

$form = $this ->createForm(new UserType(), $user, array('method' => 'PUT'));

The method bit was missing. Apparently, Symfony isn't cool enough to just get the parameters for you. When creating a form, you have to manually tell it what type of request it's dealing with. While this worked, I'm not sure that this is the best answer. I'm more than willing to hear others.

bstempi
  • 2,023
  • 1
  • 15
  • 27
  • The documentation URL http://symfony.com/doc/current/book/forms.html#book-forms-changing-action-and-method – Alexander Nenkov Jan 22 '14 at 12:35
  • I see -- it clearly states that `POST` is the default. I think some of my confusion stemmed from an example I saw where the user did't set the method, and the form still functioned: http://williamdurand.fr/2012/08/02/rest-apis-with-symfony2-the-right-way/. Granted, I did not try it myself. – bstempi Jan 22 '14 at 19:02
0

The PUT method is streamed directly into the stdin therefore to access it you should use php://input and fopen with read.

$put = fopen("php://input", "r");

disclaimer there may be a built in method for this in Symfony, but I've forgotten.

Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110
  • The whole point of this exercise is to leverage Symfony forms, so I need to be able to use the Symfony `Request` object. – bstempi Jan 22 '14 at 06:21
  • @bstempi Yup, understood. Fair enough. I'll see if I can dig it up for you real fast. – Ohgodwhy Jan 22 '14 at 06:21