Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PUT request data in Symfony2

Tags:

rest

put

symfony

The last couple of days I'm trying to build a restful api with Symfony2(2.5 to be precise). I'm building this api according to best practices most apis follow. Therefor I use a PUT http method for updating a resource. However with the PUT method I'm experiencing a problem. Symfony detects that I'm sending data with the PUT method, but the variables I'm sending are nowhere to be found. Here are some code snippets.

The javascript/jquery ajax call

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

The php route in routes.php

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

The function in the 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;
}

This outputs

string(3) "PUT"
NULL
NULL

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

like image 276
Sander Lissenburg Avatar asked Jul 22 '26 23:07

Sander Lissenburg


1 Answers

difference is

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

for first working you use

$this->request-> ...

for not working you use

$request->request-> ...

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

like image 139
john Smith Avatar answered Jul 25 '26 15:07

john Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!