Build a simple website with Silex

I love static* websites. They’re usually faster to develop, smaller, require no database or backend and are faster to load. Of course they require more work to maintain and tweak at first, but if you’re a developer, these things should be pretty entertaining or at the very least, straightforward.

There are many options out there for building static websites; both free, like Anchor, Sculpin and Stacey; and paid, like Kirby and Statamic, among many others, like Silex.

This is a good moment to tell you that I’m aware of the difference of static websites vs flat-file CMSs vs micro-frameworks.

While I love the infinite possibilities of full-fledged CMSs and the blazing speed and simplicity of full-static websites, I feel that on most cases a good middle point is useful. That’s why I frequently choose micro-frameworks (or flat-file CMSs) over full CMSs or static site generators. One gives too many options, the other is too restrictive.

Silex is “a PHP micro-framework built on the shoulders of Symfony2 and Pimple and also inspired by Sinatra.”

It’s really simple to put together a simple website running Silex.

First, run composer init to start your project.

Then add silex/silex and twig-bridge, to use the Twig templating engine:

{
    "require": {
        "silex/silex": "^1.2",
        "symfony/twig-bridge": "^2.6"
    }
}

Then just run composer install on a Terminal.

After that, all your dependencies should be sitting in your /vendor’s app directory.

Create an index.php file with this contents:

require_once __DIR__ . '/vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

$app = new Silex\Application();

// $app['debug'] = true;

$app->register(new Silex\Provider\UrlGeneratorServiceProvider());

// Register Twig provider and define a path for twig templates
$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path' => __DIR__.'/views',
));

// Home page
$app->get('/', function() use($app) {
    return $app['twig']->render('index.html');
})->bind('index');

// Other page
$app->get('/other', function() use($app) {
    return $app['twig']->render('other.html');
})->bind('other');

// 404 - Page not found
$app->error(function (\Exception $e, $code) use ($app) {
    switch ($code) {
        case 404:
          return $app['twig']->render('404.html');
            $message = 'The requested page could not be found.';
            break;
        default:
            $message = 'We are sorry, but something went terribly wrong.';
    }

    return new Response($message);
});

$app->run();

This bootstraps Silex. Here I declared two routes: / (home) and /other, which load the views index.html and other.html respectivly, on the /views folder.

That’s it! Following the same pattern, you can define as many pages as you wish.

If you need to do more complicated stuff like sending e-mails, do some logging or build forms, you can check the Silex documentation.

Links