Silex is like ExpressJS for PHP

We had the PHP Content Repository workshop at Liip in Zurich earlier this week. During the time we also discussed some other code reuse, like utilizing parts of the Symfony2 framework in Midgard. The Liip guys mentioned Silex, a cool micro-framework written on top of Symfony2. It greatly resembles the ExpressJS framework that we already use in some of our Node.js projects.

Here is a simple example of registering a route and displaying something when it is called:

<?php
$app->get('/hello/{name}', function($name) { 
    return "Hello $name"; 
});

Compare this to same in Express:

app.get('/hello/:name', function(req, res){
    res.send('Hello ' + req.params.name);
});

As we prefer to run Midgard on top of AppServer-in-PHP instead of regular mod_php, a good first step with Symfony was to figure out how it would integrate with persistent PHP processes.

It seems this is indeed easy. In couple of hours, without prior Symfony2 experience, I wrote a simple Silex extension that handles the communications between Silex/Symfony and AiP. An example for using it can be found from my GitHub fork.

Running it under siege shows one of the benefits of AiP. While the first request is a bit slower, the later ones are really fast as the application server will load the Symfony classes only once:

HTTP/1.0 200   0.03 secs:      11 bytes ==> /hello/World
HTTP/1.0 200   0.00 secs:      11 bytes ==> /hello/World
HTTP/1.0 200   0.01 secs:      11 bytes ==> /hello/World
HTTP/1.0 200   0.00 secs:      11 bytes ==> /hello/World

AppServer integration to Symfony needs to be cleaned up before it is ready for general consumption. But this example already shows that there is quite a bit of potential in the combination. If you're interested in helping, please contribute to the codebase.


Read more Midgard posts.