Version

Documentation » Page

16. Assets »

« 14. Performance

15. API

SonataPageBundle embeds Controllers to provide an API through FOSRestBundle, with its documentation provided by NelmioApiDocBundle.

15.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
    fos_rest:
        param_fetcher_listener: true
        body_listener:          true
        format_listener:        true
        view:
            view_response_listener: force
        body_converter:
            enabled: true
            validate: true
    
  • YAML
    sensio_framework_extra:
        view:    { annotations: false }
        router:  { annotations: true }
        request: { converters: true }
    
  • YAML
    twig:
        exception_controller: 'FOS\RestBundle\Controller\ExceptionController::showAction'
    
  • YAML
    # for SonataPageBundle > 2.3.6
    jms_serializer:
        metadata:
            directories:
                -
                    path: "%kernel.root_dir%/../vendor/sonata-project/datagrid-bundle/Resources/config/serializer"
                    namespace_prefix: 'Sonata\DatagridBundle'
    

In order to activate the API’s, you’ll also need to add this to your routing:

  • YAML
    # config/routes.yaml
    
    NelmioApiDocBundle:
        resource: "@NelmioApiDocBundle/Resources/config/routing.yml"
        prefix:   /api/doc
    
    sonata_api_page:
        type:         rest
        prefix:       /api/page
        resource:     "@SonataPageBundle/Resources/config/routing/api.xml"
    

15.2. Serialization

We’re using serializations groups from JMSSerializerBundle to customize the inputs & outputs.

The taxonomy is as follows:

  • sonata_api_read is the group used to display entities
  • sonata_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.

Found a typo? Something is wrong in this documentation? Just fork and edit it!