Search code examples
restsymfony5fosrestbundle

FOSRestBundle routes versioning


I wanted to implement versioning of endpoints for my project.. turns out the FOSRestBundle implementation is not quite clear so I wanted to implement like follow:

Symfony 5.4

FOSRestBundle 3

see below annotation.yaml file:

v2_controllers:
    resource: ../../src/Controller/V2/
    type: annotation
    prefix: /v2
    name_prefix: 'api_v2_'


controllers:
    resource: ../../src/Controller/
    type: annotation
    name_prefix: 'api_'

kernel:
    resource: ../../src/Kernel.php
    type: annotation

when I run this command php bin/console debug:router --show-controllers I get the following result:

..
..
...
  api_offer_list                         GET      ANY      ANY    /offer-list/{productId}/{pageType}/           App\Controller\V2\OfferListV2Controller::offerListAction()            

  api_v2_offer_list                      GET      ANY      ANY    /v2/offer-list/{productId}/{pageType}/        App\Controller\V2\OfferListV2Controller::offerListAction()            
...
..

You can see that the new version is also overriding the older endpoint for some weird reasons

and finally here's my fos_rest.yaml config:

# Read the documentation: https://symfony.com/doc/master/bundles/FOSRestBundle/index.html
fos_rest:
    param_fetcher_listener:  true

Solution

  • Inside the controller directory, you need to create two separate directories, one for v1 and the other for v2 and this should solve the problem.

    Controller/
        v1/
        v2/