Microservices with PHP and Lumen

Breaking with PHP’s past without breaking your existing applications

Randal Kamradt Sr
Level Up Coding
Published in
9 min readOct 21, 2020

--

Photo by Mimi Thian on Unsplash

PHP was developed in 1994 by Rasmus Lerdorf as a simple way to intermix code executed on the server and standard HTML. It was translated to pure HTML on the server and sent as a response to an HTTP request. It was widely adopted in the early web and was the original language of Facebook. Over time, many frameworks were developed to formalize a Model View Controller (MVC) system to allow a more controlled way of expressing modern web applications.

My previous series of articles, starting with Modern PHP, used this approach with the Symfony framework. I was able to get a decent web application running and even deployed to the web in a relatively short amount of time. But this approach is not without its shortcomings. The biggest is that the data is not available to other applications in a controlled fashion.

In this article, I’m going to take some of the backend portion of the application I created and rewrite it as a microservice in PHP. This will completely separate it from the front end and allow other entities (think mobile) to consume the same data. I’m not saying that PHP is the best language to write microservices in (I’m not going to step on the land-mine of debating what one is). I’m writing this in PHP because so many developers have a lot of knowledge invested in PHP, and it might help them transition from the world of MVC to the world of microservices.

Since this article will build on the work I did in the article Modern PHP and the subsequent two articles (all links at the end of the article), you should go back and read through and understand them. they will set up the deployment framework, database, and create the database objects that we will use here. Since many services might use the same database, separating out database initialization from the service is a must. At the end of this article you will have a partial REST API deployed to Google Cloud Platform (or any cloud platform that has basic VMs)

So what are microservices, and why would you want to use them? As the name implies, they are minimal, focused services that only handle a fraction of the backend logic. Why would you want to decompose your services to this level? It’s the very…

--

--