Search code examples
symfonysymfony4fosrestbundle

Symfony - Prefix all my routes with a version


Hi I would like to version my API. So I would like t prefix my roote with a version. like that?

# config/routes/annotations.yaml

controllers:
    resource: ../../src/Controller/
    type: annotation
    prefix:
        name: '{version}'

It works bit I would like to add a requirement to check the version.

Like:

requirements:
     version:  v1|v2

I don't find any documentation talking about that. Is there a good way to do it?


Solution

  • In the FOSRestBundle there is a complete chapter about versioning.

    https://symfony.com/doc/master/bundles/FOSRestBundle/versioning.html

    The FOSBundle has an option for versioning.

    #app/config/config.yml
    fos_rest:
        versioning:
            enabled: true
            resolvers:
                query: true # Query parameter: /users?version=v1
                custom_header: true # X-Accept-Version header
                media_type: # Accept header
                    enabled: true
                    regex: '/(v|version)=(?P<version>[0-9\.]+)/'