Request Body Converter Listener

2.0 version
Maintained

Request Body Converter Listener

ParamConverters are a way to populate objects and inject them as controller method arguments. The Request body converter makes it possible to deserialize the request body into an object.

This converter requires that you have installed SensioFrameworkExtraBundle and have the converters enabled:

1
2
3
# app/config/config.yml
sensio_framework_extra:
    request: { converters: true }

To enable the Request body converter, add the following configuration:

1
2
3
4
# app/config/config.yml
fos_rest:
    body_converter:
        enabled: true

Note

You will probably want to disable the automatic route generation (@NoRoute) for routes using the body converter, and instead define the routes manually to avoid having the deserialized, type hinted objects ($post in this example) appear in the route as a parameter.

Now, in the following example, the request body will be deserialized into a new instance of Post and injected into the $post variable:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;

// ...

/**
 * @ParamConverter("post", converter="fos_rest.request_body")
 */
public function putPostAction(Post $post)
{
    // ...
}

You can configure the context used by the serializer during deserialization via the deserializationContext option:

1
2
3
4
5
6
7
/**
 * @ParamConverter("post", converter="fos_rest.request_body", options={"deserializationContext"={"groups"={"group1", "group2"}, "version"="1.0"}})
 */
public function putPostAction(Post $post)
{
    // ...
}

Validation

If you would like to validate the deserialized object, you can do so by enabling validation:

1
2
3
4
5
6
# app/config/config.yml
fos_rest:
    body_converter:
        enabled: true
        validate: true
        validation_errors_argument: validationErrors # This is the default value

The validation errors will be set on the validationErrors controller argument:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
/**
 * @ParamConverter("post", converter="fos_rest.request_body")
 */
public function putPostAction(Post $post, ConstraintViolationListInterface $validationErrors)
{
    if (count($validationErrors) > 0) {
        // Handle validation errors
    }

    // ...
}

You can configure the validation groups used by the validator via the validator option:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
/**
 * @ParamConverter("post", converter="fos_rest.request_body", options={"validator"={"groups"={"foo", "bar"}}})
 */
public function putPostAction(Post $post, ConstraintViolationListInterface $validationErrors)
{
    if (count($validationErrors) > 0) {
        // Handle validation errors
    }

    // ...
}

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.