Symfony2 PUT request data

For the last couple of days I have been trying to create a calm api with Symfony2 (2.5 to be exact). I am building this api according to best practices. So I am using the PUT http method to update the resource. However, with the PUT method, I got a problem. Symfony detects that I am posting data using the PUT method, but the variables I am posting are not found anywhere. Here are some code snippets.

Javascript / jquery ajax call

$.ajax({
    url: 'http://www.adomain.com/app_dev.php/api/account/1',
    type: 'PUT',    
    data: 'name=Sander',
    dataType : 'json',
}); 

      

Php route to route.php

$collection->add('testbundle_api_update_account', new Route('/api/account/{id}',
array('_controller' => 'TestBundle:Account:apiUpdateAccount'),
array(), array(), '', array(), array('PUT')));

      

Function in AccountController

public function apiUpdateAccountAction($id) {
    $request = Request::createFromGlobals();
    var_dump($request->getRealMethod());
    var_dump($request->request->get('name'));
    var_dump($request->query->get('name'));
    die;
}

      

Outputs

string(3) "PUT"
NULL
NULL

      

Everything works fine. The route is found, the function is called. But where is the send data? Any ideas?

+3


source to share


2 answers


This problem is addressed by John Smith . This is probably due to the fact that in php, PUT data can only be retrieved once:

parse_str(file_get_contents('php://input', false , null, -1 , $_SERVER['CONTENT_LENGTH'] ), $_PUT):

      

In Symfony, this is probably done before calling the function in the controller. So the following doesn't work for PUT:



$request = Request::createFromGlobals();

      

The following works:

$request = $this->getRequest(); 

      

0


source


Difference

var_dump($this->request->getRealMethod());
var_dump($request->request->get('name'));
var_dump($request->query->get('name'));

      

for the first job you use

$this->request-> ...

      



for not working,

$request->request-> ...

      

try everything with $ this-> request -> ...

+1


source







All Articles