Silex framework in 5 minutes

Silex is based on some of the Symfony components and is a simple and easy framework. In this tutorial I’ll show how to install and get Silex up and runnning in 5 minutes.

I assume you have composer and PHP (> 5.5) installed, when this is the case you can just run the following command in a directory of your choice:

composer require silex/silex

This creates a composer.json and loads all the needed libraries. It also prepares the autoloading so you only have to include the vendor/autoload.php and the autoloading takes care of the rest.
Next, create a file index.php with the following content. This file implements a very simple route /person/{id} that returns a json result.

Now run the internal webserver of PHP to try the Silex framework. The URL rewriting is handled by the internal webserver so no need to take care of that.

php -S localhost:8090

When executing this with curl it returns the following JSON.

curl http://localhost:8090/person/12
{ name: "Person Name" }

When testing it with the little tool Fetcher it will look like this.

screenshot

Done

Within 5 minutes you can have a working (micro)framework and a working REST API. With Silex it could be easy