Create Kick-ass Website in no Time with Silex

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.

  • Tom

    It’s great for the type of websites that a client wants making fast and they are your best client so you don’t want to let them down! Great little blog post and thanks for attaching the example source code.

    Tom

  • Laurynas

    great post on Silex, thanks!

  • kbsali

    Very cool! I’m actually doing the same right now (move a simple web app to Silex) and was having troubles understanding how the form works… with your example things should get easier! :)

    Typo : Twig – template angine -> Engine! ;)

    Thanks!

    • Anonymous

      I’m glad you find this useful. Thanks for your feedback about the typo :)

  • Pingback: PHPDeveloper.org: Sasa Stamenkovic's Blog: Create Kick-ass Website in no Time with Silex

  • Infamous

    Silex talks too much (too verbose for my taste) like its parent. I need something more lightweight than this.

  • Pingback: Silex + form validation | SeekPHP.com

  • Anonymous

    It seems $form->isValid() will always return true. Do you know how to fix form validation?

  • Peter

    Awesome, thanks for the writeup.

  • http://twitter.com/pmdesignweb Web Design Tutorials

    how does your composer.json looks like plz…. I have some dependency issues ;S

    • umpirsky

      I worked with submodules here, but you can add this submodules into your composer.json and fix it. Maybe I will update it to use composer soon.

  • http://twitter.com/menjaraz menjaraz

    A bit off topic but I can’t help asking it: Is this bog powered by Silex ? Thanks in advance.

    • umpirsky

      No, it’s a WordPress. I know it’s slow, server problems.

  • Decebal Dobrica

    I had some problems until i could replicate your repo, so many submodules …. i cloned it using –recursive, but my app is kind of huge for a silex powered website i think, nevertheless congrats on your work, i , as a begginerer in silex and symfony, am very pleased to learn from your work.

  • Wesam Alalem

    Great article and thanks for the code.

  • Francois

    Great Post ! Thanks !