We are constantly looking for ways to do things better with a minimum of effort. That moves the technology forward, that’s why new programming languages and frameworks are written.
Last week I needed a small website. I wanted it to be quick and dirty. Well, blazing fast and not so dirty. I heard best about Silex, so I gave it a shot. It was more then good experience. It worked like a charm, it was fun to use and site was completed in one day.
Silex
Silex is a PHP microframework for PHP 5.3 developed by Fabien Potencier and Igor Wiedler. It is built on the shoulders of Symfony2 and Pimple and also inspired by Sinatra.
Code speak for itself, so lets jump into example from Silex website:
require_once __DIR__.'/silex.phar';
$app = new Silex\Application();
$app->get('/hello/{name}', function($name) use($app) {
return 'Hello '.$app->escape($name);
});
$app->run();
Can it be any easier? So, you include Silex as phar archive, create application, define a route and match it to function to return response and finally just run your application.
The entire idea is not very new, as I already said, it is inspired by Sinatra, application framework written in Ruby. There are many PHP micro-frameworks as well, like Fat-Free, Limonade, Slim… But let me highlight why Silex is my choice:
- Based on Symfony components

- DI container
- High quality codebase
- Powerful extension mechanism
- Testable
- Fun to use
Working Example
I have a simple portfolio website umpirsky.com built in Zend Framework and I decided to migrate it to Silex. Site itself has few static pages and a contact form, perfect candidate for trying the microframework out.
To get started I added few git submodules:
- Silex – core framework.
- Twig – template engine.
- Swiftmailer – for sending emails from contact form.
- Symfony – form component.
I put everything in app.php file because site is pretty simple, but Silex lets you organise your code the way you want. Most of it (the first half) is bootstrapping Silex and registering extensions. Adding static pages was as simple as:
$pages = array(
'/' => 'about',
'/portfolio' => 'portfolio',
'/social' => 'social'
);
foreach ($pages as $route => $view) {
$app->get($route, function () use ($app, $view) {
return $app['twig']->render($view . '.twig');
})->bind($view);
}
Since I already had HTML/CSS, I splitted it into twig layout and few templates. Adding contact form was the only “problem”, but with the help of form extension and Swiftmailer it took less then 30 lines of code.
Building this website took several hours, and more important, it was lots of fun. Source code is on github, I hope someone will find this example useful.
Pingback: PHPDeveloper.org: Sasa Stamenkovic's Blog: Create Kick-ass Website in no Time with Silex
Pingback: Silex + form validation | SeekPHP.com