- GET http://0.0.0.0:8080/pet
- GET http://0.0.0.0:8080/pet/findByStatus
- GET http://0.0.0.0:8080/pet/{id}
- POST /pet
- PUT /pet
- POST /{id}
- DELETE /{id}
- POST /{id}/uploadImage
This is a basic example of a Microservice in PHP. I've used the Slim framework as the base. I've chosen Slim because it's just a router / container which will make it easier to distinguish my code from the framework. The small amount of auto-gen code it produces is quite well done and easy to extend.
Because of the simplicity of this service, I have not been able to show the following concepts:
- Dispatch async events to some sort of event bus
- Separate writes from reads (CQRS)
- Implement circuit breaker pattern to gracefully handle when services are down
I've assumed that the resource permissions will be applied before fetching the data from this service. So I won't cover any search restriction code in this example.
A read request will take the following steps:
- Safely apply search filters
- Fetch data from repository
- Transform the response into a nice API response (usually I'd use the Fractal library but the source API docs do not have pagination so there's no point)
A write request will have the following duties to perform during the life of the request.
- Validation
- Write to a log table (useful for diagnostics or a history feature)
- Create/Update the state in the main read table
- Emit an event so other services can use that information (Kafka, SNS + SQS, etc.)
- Return response code
I've kept the database interaction logic very simple. There's plenty of packages that can do it with plenty of depth but that's not where I'm going to put my effort for this example.
You can use docker-compose to run the app:
cp .env.example .env
composer install
docker-compose up -dAfter that, open http://0.0.0.0:8080 in your browser or with Postman.