Silex is a microframework derived from it’s big brother Symfony. It’s described as a simple framework to develop simple websites but as it’s in active development, getting it up and running can be a little tricky. We’re going to do just that and then go through a quick example application to get things moving.
Getting the Source
As Silex draws on part of the Symfony framework, there’s quite a lot of source code to get down and into the right places. It uses git’s submodules system to pull in various other repositories that it requires. If you download our compiled Silex Phar file, you can pick up at the “Sample Application” step.
If you’re running one of the latest versions of git, you can use the “–resursive” option to sort the submodules out, but it’s more likely that you’ll need to take a couple of extra steps:
git clone https://github.com/fabpot/Silex.git
cd Silex
git submodules init
git submodules update
To compile to a Phar file, run the following:
php Silex/compile
If you run into problems at this stage you may need to alter a couple of the configuration options for Phar. Open up your “php.ini” file and make sure the following options are set:
phar.readonly = Off
phar.require_hash = Off
Sample Application
With the compile script run without any problems, we have an archive called “silex.phar” in the folder we were working from. We copy this file into the root of our web application and create an “index.php” to hold our code.
Silex makes it very simple to create a basic application through the use of simple REST-like functions and PHP 5.3′s closures (or anonymous functions). First, we set up Silex by including the autoloader, telling it that we’re using the namespaced Silex\Application class and then instantiating that class:
require 'phar://silex.phar/autoload.php';
use Silex\Application;
$app = new Application();
We can now start to configure the routes of our application, starting with a standard output for “/”, the document root. At the end of the script we call the application’s run method to kick things off.
$app->get('/', function() {
return "Index Page";
});
$app->run();
Now if we visit “http://localhost/app/index.php”, we’ll see the message “Index Page” in the browser. The real flexibility comes when we use variables in our routing and in our closures. We add this code to our script before the application’s run method.
$app->get('/hello/{name}', function($name) {
return 'Hello '.$name;
});
By creating a route with part of it surrounded by braces, we annex that part as a variable. This then gets passed into our function so it can be used like any other variable within a function. If we visit our application at “http://localhost/app/index.php/hello/Kryten”, we’ll see the message “Hello Kryten” in the browser.
Futher Reading
We love Silex so we’ll be featuring it a lot over the coming months as it matures. In the meantime, there’s the Silex page on GitHub and slides from this Silex presentation by Igor Wielder. Be careful though as some of the examples used in the presentation are no longer valid.