In addition to being the domain I landed this blog on, Fliglio is the name of the PHP Framework
we built and have been using at work for several years now. Recently we went through the
effort of cleaning it up to make available on github.
In this post I will attempt to show you how to use Fliglio with a Getting Started guide for RESTful microservices.
Running The App
Before we go any further, grab a copy of the demo application here.
git clone https://github.com/fliglio/rest-gs.git
This demo requires MySQL to back the todo entries (along with
apache or nginx and the appropriate version of php).
If that sounds like a pain in the ass, you can also use Docker. The demo repo comes with a
Makefile to easily run Fliglio services in a Docker container.
This will pull down the Docker image fliglio/local-dev
and run the app in a container. It’s spitting out logs to stdout and will keep running until you hit CTRL+c.
With that going, open a new terminal and run:
make migrate
This will apply the phinx database migrations for the app (see db/migrations).
Now you can explore the service:
Add a todo
$ curl -s -X POST localhost/todo -d '{"description": "take out the trash", "status": "new"}' | jq .
query for that todo we just created
$ curl -s localhost/todo/1 | jq .
Cool! Let’s look at the code now.
Code Overview
The project uses Composer to manage its dependencies.
The /httpdocs directory contains a lightweight index.php to bootstrap the application
The application class is the application’s entry point and is responsible for managing configuration classes as well as actually dispatching a request.
The configuration class is responsible for things like specifying how to route a request and for instantiating application dependencies.
Typically, a microservice will also have one or more resources classes to organize behavior for individual urls.
Todo, A Microservice
I’m sure I could explain everything about the framework, but I’m also sure you wouldn’t care.
So let’s just jump in and look at the service.
Defining a resource
This should be pretty familiar if you’re used to REST apis. We’re defining methods
on this class to handle providing basic CRUD functionality for todos with http verbs.
By hinting the parameters to these methods with classes like PathParam, GetParam, and Entity, we can specify
what parameters we need to perform an action (and allow the framework to inject them rather than parsing
the Request directly.)
(Don’t worry too much about the method getWeatherAppropriate, this is a contrived
example to aid in showing how to test all of this.)
Those comments aren’t magical however; we still need to map this functionality to urls. We manage this in the
configuration class
And there you have it! A framework for developing REST services.
Next Steps
That wasn’t a terribly in depth introduction to Fliglio, but I’ve gotta start somewhere, right?
comments powered by Disqus