Table des matières

FOSRestBundle

Page officielle : https://symfony.com/doc/master/bundles/FOSRestBundle/index.html

Installation

FOS-RestBundle a besoin de JMS-SerializerBundle,
il faudra donc installer ce bundle avant FOS-RestBundle, sinon → ERROR
  1. Installer JMS-Serializer : composer require jms/serializer-bundle
  2. Se deplacer dans le répértoire du projet et lancer la commande composer require friendsofsymfony/rest-bundle
D:\webprojects\api2-test>composer require friendsofsymfony/rest-bundle
Using version ^2.6 for friendsofsymfony/rest-bundle
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Restricting packages listed in "symfony/symfony" to "4.3.*"

Prefetching 5 packages
  - Downloading (100%)

Package operations: 5 installs, 0 updates, 0 removals
  - Installing willdurand/jsonp-callback-validator (v1.1.0): Loading from cache
  - Installing willdurand/negotiation (v2.3.1): Loading from cache
  - Installing doctrine/inflector (v1.3.0): Loading from cache
  - Installing symfony/security-core (v4.3.6): Loading from cache
  - Installing friendsofsymfony/rest-bundle (2.6.0): Loading from cache
Writing lock file
Generating autoload files
Symfony operations: 1 recipe (075c3c4d2711920c92f2b20b74b94fb5)
  -  WARNING  friendsofsymfony/rest-bundle (>=2.2): From github.com/symfony/recipes-contrib:master
    The recipe for this package comes from the "contrib" repository, which is open to community contributions.
    Review the recipe at https://github.com/symfony/recipes-contrib/tree/master/friendsofsymfony/rest-bundle/2.2

    Do you want to execute this recipe?
    [y] Yes
    [n] No
    [a] Yes for all packages, only for the current installation session
    [p] Yes permanently, never ask again for this project
    (defaults to n): y
  - Configuring friendsofsymfony/rest-bundle (>=2.2): From github.com/symfony/recipes-contrib:master
Executing script cache:clear [OK]
Executing script assets:install public [OK]

Some files may have been created or updated to configure your new packages.
Please review, edit and commit them: these files are yours.

Configuration

Fichier config fos_rest.yaml

config\packages\fos_rest.yaml
fos_rest:
    body_converter:
        enabled: true
    view:
        formats:
            json: true
            xml: false
            rss: false
    serializer:
        serialize_null: true

Vérification de la configuration

avec symfony console debug:config fos_rest

Si on obtient l'erreur
«You need to enable the parameter converter listeners in SensioFrameworkExtraBundle when using the FOSRestBundle RequestBodyParamConverter»

Un simple composer require sensio/framework-extra-bundle résoud ce message.

Ceci est dû a l'utilisation de fos_rest.body_converter.enabled=true

Plus d'infos ici : https://symfony.com/doc/master/bundles/FOSRestBundle/request_body_converter_listener.html