Skip to main content

HOWTO: Quick start to the Silex microframework

·4 mins

Because I work on rather large projects, I usually use the full-stack Symfony2 framework created by Sensiolabs. This framework helps me to do my job better and more efficient. Even if I'm working on a rather small project, which doesn't need al the fancy features of Symfony2, I use it because of the already existing boilerplate and setup. This is why I'm interested in the Silex microframework.

How to start with the Silex microframework (also created by Sensiolabs) and what is the best folder structure for my new micro project? Edwin Kortman (also known as Dreadwin) created a Silex boilerplate repository based on a blog post by Matthias Noback in 2012 to quickly setup a new Silex project and I'm really excited about it!

What is Silex?

Silex is a PHP microframework for PHP. It is built on the shoulders of Symfony2 and Pimple and also inspired by sinatra.
- http://silex.sensiolabs.org/

Silex is actually a PHP application where controller and route mappings are done in a single file: app/app.php. You can use it for your micro project, micro service, a proof of concept or just "to test some things".

An example from the Silex website:

$app = new Silex\Application(); 

$app->get('/hello/{name}', function($name) use($app) { 
    return 'Hello '.$app->escape($name); 
}); 

$app->run(); 

About the Silex boilerplate repository

  • The Silex boilerplate repository is the thin Silex implementation without Twig or Doctrine. I will discuss the implementation of Twig later on in this blog post.
  • PHPUnit ^4.0 is available in your development environment.
  • Composer!
  • The /src directory represents the App namespace (PSR-4) and is autoloaded by composer.

Requirements

  • PHP >5.3.9
  • ext-xdebug when running PHPUnit code coverage in development environment

Pre-requirements

Get a copy of the repository by cloning it with your favorite GIT tool:

$ git clone https://github.com/Dreadwin/silex-boilerplate.git silex

If you want to commit any changes in a new repository you can change the remote (or add a separate one):

$ git remote set-url origin <new-url>

Before booting up Silex for the first time, install all dependencies with composer.

$ composer install

Run your Silex application

Hopefully you're not using PHP 5.3.x anymore. If that is the case you can use the built-in webserver of PHP for development. This server allows you to run your Silex application without any configuration - Easy!

$ php -S 127.0.0.1:8080 -t web web/index.php

After booting up, the application is available at http://127.0.0.1:8080. After requesting the page it should show you "Welcome to my new Silex app" and your application is ready to use!

Twig integration

Most visitors will appreciate it if the frontend work is also top of the art. Twig is a flexible, fast and secure templating engine that's also developed by Sensiolabs and can help you to create dynamic and awesome views. But before you can use Twig, you have to implement it in your Silex application.

First, load the twig dependency with composer:

$ composer require twig/twig

Secondly, register it in app/app.php and define the path where your views will live:

$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path' => __DIR__.'/views',
));

If the views directory doesn't exist, create it:

$ mkdir app/views

Create the first view:

$ vi app/views/hello.twig

And add some content:

<h1>Hello {{ name }}!</h1>

And define the controller and route mapping:

$app->get('/hello/{name}', function ($name) use ($app) {
    return $app['twig']->render('hello.twig', array(
        'name' => $name,
    ));
});

Test it by going to http://127.0.0.1:8080/hello/world and it should show you "Hello world!".

The full documentation about the twig integration can be found an the Silex website and includes additional integration between some Symfony components and Twig like URL generation, translations, form helpers and security functions.

Static files

If you use the built-in PHP webserver you will notice that Silex tries to match every request. In order to do frontend work you will probably need to serve stylesheets, images, etc. To achieve this you will have to make sure that your front controller (web/index.php) returns false in that case.

Add this snippet at the start of the app/index.php before requiring the app.php.

$filename = __DIR__.preg_replace('#(\?.*)$#', '', $_SERVER['REQUEST_URI']);
if (php_sapi_name() === 'cli-server' && is_file($filename)) {
    return false;
}

Links

Silex boilerplate: https://github.com/Dreadwin/silex-boilerplate
Silex with Twig: https://github.com/piwi91/silex-boilerplate-with-twig
Silex documentation: http://silex.sensiolabs.org/doc

If you have any questions please leave a comment!

And if you find any issues or you have additions, don't forget to create a PR! :-)