Category: Symfony

Reading incoming JSON bodies in Symfony

Accepting JSON encoded data from requests is crucial when developing APIs. By default, Symfony will not unserialize the object as an array, and you need a little workaround to make it available as you would with any form submitted request.

We can make use of an EventSubscriber to have the JSON content always decoded and put in $request->request.

(more…)

How to containerize any Symfony application using Docker in seconds

tl;dr: Customize your dependencies using phpdocker.io and put the result files in the root or your project, then edit your .env files accordingly and run docker-compose up -d. 🐳

Personal background

When my company started to migrate from PHP 5 to PHP 7 I had to work on different applications with different language versions. At the time I used to have a personal virtual container that ~reflected the configuration of a production machine, but it was slow because I used to develop on it using sshfs. So, it was common for me to have some applications running locally on my machine, but handling two different PHP versions was not easy.

The solution was to start considering having each project completely platform independent, and my main objective was to be able to reach a point were the PHP version installed on my Mac was irrelevant.

Introduction

We will consider a very basic Symfony application running on any version of PHP with any database attached. Of course, the database is not required, but I will show you how to attach an external dependency to the project.

In the end you will have a project that can be installed on any machine running Docker Desktop (or docker-compose on Linux), with PHP 7.4 and MySQL 8.

(more…)