PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

A silex love story - 'Embedded' PHP

Note: This article was originally published at Planet PHP on 5 December 2012.
Planet PHP

A couple of weeks ago Igor wrote a fantastic blog post about aoScaling a Silex code basea which made me remember a story I wanted to share about how I fell in love with Silex.

This blog post aims to tell that story.

If you never heard of Silex is let me paste the blurp from the silex home page for you: aoSilex is a PHP microframework for PHP 5.3. It is built on the shoulders of Symfony2 and Pimple and also inspired by Sinatra.a

To give you some context of how and why that little thing left so much impact with me I've got to give you some context of what I was doing at that time.

2011. Thats when we migrated things to PHP 5.3

Back in early 2011, which feels a lot longer ago than it sounds when i write it down, I was working on a legacy code base (well who wasn't?) that was a littler larger than most other things I've since come in contact with. The core application was around 500kloc and all things combined it was around 3mloc.

We'd spent quite some time moving from PHP 4.3 to 5.2 and it was time to move into 5.3 but at the same time we wanted to get some stuff done and we had an interesting new project coming around.

Shipping hardware with a LAMP stack

To not bore you with details: Basically we had to ship a smaller version of the existing web application on aembedded' hardware so that our customers could run it locally. It would receive data from local hardware, process it and create fancy near-time report pages from it.

I'm putting the aembedded' in quotes because we're talking about something that had a basic intel cpu, a of gig ram, an SD Card slot and didn't need active cooling. Think Raspberry Pi but a little bigger and with a box around it.

Why?

We sure could have done that with another language and using Python would have been an option if it weren't for all of the massive amount of existing business logic that he had written in PHP and didn't want to port for time and consistency reasons. Since the box wouldn't necessarily talk to our servers using a server side API wasn't an option either.

So let's slap a BSD and Apache on it and let's go

But we need a framework and using our legacy thing would require loading at least an 1mloc on it.. for a couple of routes and pagesa and maybe not using our static, very specific Request abstractiona can't we do something more a less?

In the month leading up to this I was building some small fun projects and was looking for stuff I could use and I came across some amicroframework' blog post.

Mostly those consited of people arguing that astatic methods aren't that bad' (spoiler: they are).

It was the early 5.3 time where everyone and their mother was trying to promote a framework that was marketed as aosimple and flexiblea and that where neither simple nor offered any freedom or flexibility. But then someone sent me a link to Silex.

The thing I saw first and that made me fall in love with Silex was:

require_once __DIR__.'/silex.phar'; $app = new Silex\Application(); $app-get('/hello/{name}', function($name) use($app) { return "Hallo $name"; }); $app-run();

It was 6 lines of code. Six!

It was incredibly easy to understand and straight forward.

Testing it would be no issue at all since I could just either not test the glue code in the closures or test them on their own; I didn't even need to look into their docs to know that it would just work out.

And the part that is maybe easier to overlook: It was a PHAR! Which meant I didn't have to care about an

Truncated by Planet PHP, read more at the original (another 14440 bytes)