5. API¶
SonataNewsBundle embeds a Controller to provide an API through FOSRestBundle
,
with its documentation provided by NelmioApiDocBundle
.
5.1. Setup¶
If you wish to use it, you must first follow the installation instructions of both bundles:
Here’s the configuration we used, you may adapt it to your needs:
- YAML
1 2 3 4 5 6 7 8 9 10 11
# config/packages/fos_rest.yaml fos_rest: param_fetcher_listener: true body_listener: true format_listener: true view: view_response_listener: force body_converter: enabled: true validate: true
- YAML
1 2 3 4 5 6
# config/packages/sensio_framework_extra.yaml sensio_framework_extra: view: { annotations: false } router: { annotations: true } request: { converters: true }
- YAML
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# config/packages/jms_serializer.yaml jms_serializer: visitors: xml_serialization: format_output: '%kernel.debug%' metadata: directories: SonataNewsBundle: namespace_prefix: 'Sonata\NewsBundle\' path: '@SonataNewsBundle/Resources/config/serializer' SonataDatagridBundle: namespace_prefix: 'Sonata\DatagridBundle\' path: '@SonataDatagridBundle/Resources/config/serializer'
In order to activate the ReST API, you’ll also need to add this to your routing:
1 2 3 4 5 6 7 8 9 10 | # config/routes.yaml
NelmioApiDocBundle:
resource: "@NelmioApiDocBundle/Resources/config/routing.yml"
prefix: /api/doc
sonata_api_news:
type: rest
prefix: /api
resource: '@SonataNewsBundle/Resources/config/routing/api.xml'
|
5.2. Serialization¶
We’re using JMSSerializerBundle’s serializations groups to customize the inputs and outputs.
The taxonomy is as follows:
sonata_api_read
is the group used to display entitiessonata_api_write
is the group used for input entities (when used instead of forms)
If you wish to customize the outputted data, feel free to setup your own serialization options by configuring JMSSerializer with those groups.