Skip to content
Example of API versioning on Symfony Framework v4
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin
config
public
src
tests
.env
.env.test
.gitignore
README.md
composer.json
db.sqlite
grumphp.yml
php_cs.php
phpstan.neon
phpunit.xml
symfony.lock

README.md

sf4-api-versioning

Example of API versioning on Symfony Framework v4

Used additional bundles

  • friendsofsymfony/rest-bundle
  • jms/serializer-bundle
  • sci3ma/sf4grumphp (optional)

Steps to create project

  1. Create Resource/config/api_routing.yaml for controllers routes
  2. Include Resource/config/api_routing.yaml into config/routes.yaml
    1. Add proper prefix with version parameter
    2. Add available/supported versions in .env file
  3. Create Entity with properties:
    1. id
    2. name
    3. age
    4. color
  4. Create Controller and define RESTful actions

Run project

  1. Run build-in php server bin/console server:start
  2. Go to:
    1. [GET] 127.0.0.1:8000/api/v1.1/godzillas/1 and see response with properties:
      1. id
      2. name
      3. age
    2. [GET] 127.0.0.1:8000/api/v1.2/godzillas/1 and see response with properties:
      1. id
      2. name
      3. age
      4. color
    3. [GET] 127.0.0.1:8000/api/v1.3/godzillas/1 and see response with properties:
      1. id
      2. name
      3. age
      4. full_name - which is virtual property
  3. Similarly as example above:
    1. [GET] http://127.0.0.1:8000/api/v1.1/godzillas
    2. [GET] http://127.0.0.1:8000/api/v1.2/godzillas
    3. [GET] http://127.0.0.1:8000/api/v1.3/godzillas

Conclusion

The main goal of versioning api is changing/updating response structure with keeping the older version in the same time.

You can’t perform that action at this time.