Example of API versioning on Symfony Framework v4
- friendsofsymfony/rest-bundle
- jms/serializer-bundle
- sci3ma/sf4grumphp (optional)
- Create
Resource/config/api_routing.yaml
for controllers routes - Include
Resource/config/api_routing.yaml
intoconfig/routes.yaml
- Add proper prefix with
version
parameter - Add available/supported versions in
.env
file
- Add proper prefix with
- Create Entity with properties:
- id
- name
- age
- color
- Create Controller and define RESTful actions
- Run build-in php server
bin/console server:start
- Go to:
[GET] 127.0.0.1:8000/api/v1.1/godzillas/1
and see response with properties:- id
- name
- age
[GET] 127.0.0.1:8000/api/v1.2/godzillas/1
and see response with properties:- id
- name
- age
- color
[GET] 127.0.0.1:8000/api/v1.3/godzillas/1
and see response with properties:- id
- name
- age
- full_name - which is virtual property
- Similarly as example above:
[GET] http://127.0.0.1:8000/api/v1.1/godzillas
[GET] http://127.0.0.1:8000/api/v1.2/godzillas
[GET] http://127.0.0.1:8000/api/v1.3/godzillas
The main goal of versioning api is changing/updating response structure with keeping the older version in the same time.