API¶
Sonata e-commerce embeds Controllers to provide an API through FOSRestBundle, with its documentation provided by NelmioApiDocBundle
.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | fos_rest:
param_fetcher_listener: true
body_listener: true
format_listener: true
view:
view_response_listener: force
body_converter:
enabled: true
validate: true
sensio_framework_extra:
view: { annotations: false }
router: { annotations: true }
request: { converters: true }
twig:
exception_controller: 'FOS\RestBundle\Controller\ExceptionController::showAction'
|
In order to activate the API’s, you’ll also need to add this to your routing:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | NelmioApiDocBundle:
resource: "@NelmioApiDocBundle/Resources/config/routing.yml"
prefix: /api/doc
sonata_api_ecommerce_product:
type: rest
prefix: /api/ecommerce
resource: "@SonataProductBundle/Resources/config/routing/api.xml"
sonata_api_ecommerce_order:
type: rest
prefix: /api/ecommerce
resource: "@SonataOrderBundle/Resources/config/routing/api.xml"
sonata_api_ecommerce_invoice:
type: rest
prefix: /api/ecommerce
resource: "@SonataInvoiceBundle/Resources/config/routing/api.xml"
sonata_api_ecommerce_customer:
type: rest
prefix: /api/ecommerce
resource: "@SonataCustomerBundle/Resources/config/routing/api.xml"
sonata_api_ecommerce_basket:
type: rest
prefix: /api/ecommerce
resource: "@SonataBasketBundle/Resources/config/routing/api.xml"
|
Serialization¶
We’re using JMSSerializationBundle's
serializations groups to customize the inputs & 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.