A small example of how an API could be written in Symfony. This project allow a user to receive/add/update/delete cars.
All output is standardized so that it is easy to parse in any language on any environment.
- Data retrieval
- Data manipulation
// GET
symfony.app/car
// GET by id
symfony.app/car/{id}
// POST new
symfony.app/car
// PATCH update
symfony.app/car/{id}
// DELETE remove
symfony.app/car/{id}
- PHP >= 7.1
- Composer
- MySQL >= 5.5
- Clone this repo
- Run
composer install
- Run
php bin/console doc:mig:mig
- Run
php bin/console doc:fixtures:load
- Run
php bin/console doc:mig:mig --env=test
- Run
composer test
For more information which tests are run, please refer to the "test"
section of composer.json
# Request
$ curl --request GET \
--url http://symfony.app/car
# Response
[
{
"id": 1,
"brand": "Ford",
"name": "Mustang",
"year": 1972
},
{
"id": 2,
"brand": "Toyota",
"name": "Corolla",
"year": 1983
},
]
# Request
$ curl --request GET \
--url http://symfony.app/car/1
# Response
{
"id": 1,
"brand": "Ford",
"name": "Mustang",
"year": 1972
}
# Request
$ curl --request POST \
--url http://symfony.app/car \
--header 'content-type: application/json' \
--data '{
"brand": "Ford",
"name": "Mustang",
"year": 1972
}'
# Response
{
"id": 1,
"brand": "Ford",
"name": "Mustang",
"year": 1972
}
# Request
$ curl --request PATCH \
--url http://symfony.app/car/1 \
--header 'content-type: application/json' \
--data '{
"year": 2016
}'
# Response
{
"id": 1,
"brand": "Ford",
"name": "Mustang",
"year": 2016
}
# Request
$ curl --request DELETE \
--url http://symfony.app/car/1
# Response
{
"message": "Car deleted"
}