modular php microservices with Slim framework

customized backend solutions with a small footprint

For building REST-based microservice backends I use Slim Framework.
Slim is a PHP micro framework which basically takes a HTTP request, calls a routine based on it and returns a response.
It relies on the same composer based environment like its bigger brothers laravel and symfony but is – how the name implies – reduced to the max.

This reduction comes with a little extra bootstrapping work. When bootstrapping is done you have a beautiful, flexible and powerful piece of software, which can provide backend-services for any modern app and also runs on almost every webhost.

The modular pattern, allows to integrate only the packages you need for your project.
For instance:

  • user management with sentinel
  • guzzle http to run a proxy, e.g. for collecting data from web-APIs or sites
  • you can borrow the ORM-Layer from laravel called Eloquent
  • use a a configurable logger like monolog
  • or the event manager from doctrine
  • do database migrations and inserting mock-data via phpmig

I used Slim for a couple of projects and do not want to miss it anymore in my toolbox.

Bootstrapping Slim with Composer
Bootstrapping Slim with Composer.

I used this setup to develop transver-database.
This server software is repeatedly querying SurveyMonkey for new or edited surveys. This is done with guzzle http. It inserts this survey data into its own database and taking care of updating procedures. Also it provides some REST-routes which are then used by a react app. See it in action here.

Screenshot from a database migration file
Migration file which creates a table containing SurveyMonkey answers and adding some comments

Handling database while development with migrations gives us a kind of revision management for databases and allows to intergrate database operations into develop- and build-pipelines.

Screenshot Behaviour-Driven-Development testing business logic
Testing business logic with behat BDD framework

For a project I integrated Behaviour-Driven-Development (BDD) into Slim via the behat testing framework.
This allowed us to test even fine-grained business logic, which does not only depend on the application but also the underlying data.