SYmfony2 request can't get the post variable

1.1k views Asked by At

I'm trying to get the variable of a POST request in a symfony 2.4 controller. I usually have no issues on that but for this one...

this is how I call the controller:

var deferred = $q.defer();
     $http({method:'POST',data:{dimensionpassed:dimension},url:Routing.generate('_API_getTree')}).success(function(result){
         deferred.resolve(result); 
      });                

return deferred.promise;

Now when I look in the console I can see the post request:

JSON
dimensionpassed
    "ChartOfAccounts"
Source
{"dimensionpassed":"ChartOfAccounts"}

And here is the controller:

public function _API_getTreeAction(Request $request)
{
$test = $request->getContent();
var_dump($test);
$test =  $request->request->get('dimensionpassed');
var_dump($test);
}

The first var dump gives me "{"dimensionpassed":"ChartOfAccounts"}"

so there is something !

but the second is only "NULL"

what am I doing wrong ?

2

There are 2 answers

1
Eagle1 On BEST ANSWER

Apparently, Angularjs $http post variable don't go to the $_POST variable, hence it's impossible to have them in the $request->request->all().

However they appear "raw" as you can have them with this:$request->getContent();

Simply using $myvar = json_decode($request->getContent()) does the trick

1
Richard On

I don't know angularjs at all, but that looks like JSON in the body, not request body parameters.

Try:

var_dump($request->request->all());

If you're sending post parameters you should see something like:

array 'dimensionpassed' => string 'ChartOfAccounts'

And the request body should look something like:

'dimensionpassed=ChartOfAccounts'

So you probably just need to get your POST from Javascript working properly (can't help there sorry).