Search code examples
restsymfonyroutesfosrestbundlesymfony-3.4

Routing for mutliple REST controllers using FOSRestBundle w/ Symfony 3.4


I'm trying to configure routing using FOSRestBundle (2.3.1) with Symfony (3.4.11). The following works as expected:

routing.yml:

fooV1:
    type:     rest
    resource: AppBundle\Controller\V1\FooController
    prefix: /api/v1

producing:

/bin/console debug:router
--------------------------------- -------- -------- ------ -------------------------------
Name                              Method   Scheme   Host   Path
--------------------------------- -------- -------- ------ -------------------------------
get_foos                         GET      ANY      ANY    /api/v1/foos.{_format}
get_foo                          GET      ANY      ANY    /api/v1/foos/{bar}.{_format}
post_foos                        POST     ANY      ANY    /api/v1/foos.{_format}
put_foos                         PUT      ANY      ANY    /api/v1/foos/{bar}.{_format}
delete_foos                      DELETE   ANY      ANY    /api/v1/foos/{bar}.{_format}
--------------------------------- -------- -------- ------ -------------------------------

So far so good. Now I want to add a V2 API:

routing.yml:

fooV1:
    type:     rest
    resource: AppBundle\Controller\V1\FooController
    prefix: /api/v1
fooV2:
    type:     rest
    resource: AppBundle\Controller\V2\FooController
    prefix: /api/v2

The problem is that the V1 routes/controller seem to completely disappear, with only the V2 remaining (note that the controllers have the same methods, it's only the response format that changes, hence the new version):

/bin/console debug:router
--------------------------------- -------- -------- ------ -------------------------------
Name                              Method   Scheme   Host   Path
--------------------------------- -------- -------- ------ -------------------------------
get_foos                         GET      ANY      ANY    /api/v2/foos.{_format}
get_foo                          GET      ANY      ANY    /api/v2/foos/{bar}.{_format}
post_foos                        POST     ANY      ANY    /api/v2/foos.{_format}
put_foos                         PUT      ANY      ANY    /api/v2/foos/{bar}.{_format}
delete_foos                      DELETE   ANY      ANY    /api/v2/foos/{bar}.{_format}
--------------------------------- -------- -------- ------ -------------------------------

My fos_rest config (in config.yml):

fos_rest:
    exception:
        enabled: true
        exception_controller: 'AppBundle\Controller\ExceptionController::showAction'
    routing_loader:
        default_format: json
    view:
        view_response_listener: force
        formats:
            rss: false
            xml: false
            json: true
            jsonp: false
    body_converter:
        enabled: true
    body_listener:
        default_format: json

Any tips on making this work without having to rewrite the whole routing/controllers? The existing documentation doesn't seem completely accurate, with documented fields that do not work.


Solution

  • Maybe the generated V2 routes replace the V1 routes, because they have the same generated names. Indeed FOSRest can generate REST routes as you already know.