Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do not work ParamConverter with "fos_rest.request_body" converter

I'm trying to use FOSRestBundle's request body converter but my current implementation doesn't seem to work.

I am using Symfony2, Propel, AngularJS (it sends data to server)

What am i doing wrong?

config.yml:

fos_rest:
    routing_loader:
    default_format: json
    include_format:       false     
view:
    view_response_listener: force
body_listener: true
param_fetcher_listener: true
body_converter:
    enabled: true

sensio_framework_extra:
    view:    { annotations: false }
    router:  { annotations: true }
    request: { converters: true }

Method:

/**
 * @View()
 * @Put("/documenttypes")
 * @ParamConverter("documentType", converter="fos_rest.request_body")
 */
public function putAction(DocumentType $documentType)
{
    print_r($documentType);
    return $documentType;
}

In result I have empty model object:

Backend\SettingsBundle\Model\DocumentType Object
(
  [id:protected] => 
  [name:protected] => 
  ....
)

To check that data comes to server, I modify method:

public function putAction(DocumentType $documentType)
  {
    $content = $this->get("request")->getContent();
    $documentType->fromArray(json_decode($content, true));

    print_r($documentType);
    return $documentType;
  }

This method works and fills model field:

Backend\SettingsBundle\Model\DocumentType Object
(
  [id:protected] => 2
  [name:protected] => 'Oferta'
  ....
)
like image 834
Dmitry Skryabin Avatar asked Feb 02 '14 15:02

Dmitry Skryabin


1 Answers

Symfony is complaining that You need to enable fos_rest.converter.request_body service.

In your fos_rest configuration section within config.yml you need to enable body_converter feature:

fos_rest:
  body_converter:
    enabled: true

Now if you list your services, you will notice that service fos_rest.converter.request_body has appeared:

user@host:~/symfony-project$ php app/console debug:container | grep fos_rest.converter
 fos_rest.converter.request_body       FOS\RestBundle\Request\RequestBodyParamConverter        
like image 58
Dimitry K Avatar answered Oct 04 '22 15:10

Dimitry K