This post aims to show the basics, and how to get started with the php micro framework Silex– which, by the way, is my favorite php framework nowadays. It’s based on Symfony 2, but focused on smaller applications. It has a really comprehensive and intelligent schema for url rewriting (so-called “Routes”) that brings to PHP one of the great things you can find in Django. It’s concise, extensible – using Pimple and Composer for dependency management – and secure. Oh, almost forgot: it’s practical, easy to learn and super versatile – according to my own experience with the framework so far.

So, lets write some code! These instructions are focused on Linux (Ubuntu) environments. Another important thing to note is that Silex requires PHP 5.3 or higher.

First things first: install Silex via Composer

Let’s start by installing Composer*, so we can download the last version of Silex. This is the best way for maintaining your project and manage dependencies, its really easy to install and update everything! Open the terminal and go to your project folder. Then:
curl -s http://getcomposer.org/installer | php
Now, you must create a composer.json file containing instructions about which packages you are going to use in your project. The basic composer.json file for Silex is bellow:

Now you can actually install everything:

php composer.phar install

You will get something like this: Note: if you get a json error when installing composer, try to edit the file and trim the whitespaces. *Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you. If you are not using Linux or have some trouble using Composer, you can also download Silex in the old-fashioned way – just go to http://silex.sensiolabs.org/download and follow the instructions.

Hello Worlding

Now that you have Silex installed, have a look at the directory tree. You will find a folder named “vendor” – that’s where Composer installs all dependencies defined at composer.json . Let’s create an index.php file at the root directory of your project and make some “Hello World” stuff. You can organize things better later. Let’s start with the basics.

This will give you a basic “Hello World” output, as you can imagine. But let’s see what happens if you remove the route definition part from the script:

Want to get more information about this error? Set the debug mode in your application to true. The code will be like this:
And you will get this: Gorgeous! Now you can debug everything – just remember to unset this option when your project is in production.

Working with Routes

Now that you are familiar with the basics, let’s take a deeper look at the Silex Routes. What if I want to get some parameters along with the request? No problem at all – there’s a quite awesome way to accomplish this:

Got it? Heads up: when somebody hits the /hello path, the app will throw an error, since the only defined route is /hello/something. You can use the value modifier to set a default value for {name}, so you don’t need to define another route for /hello. Just like this:

Note that until now we only worked with GET routes. The POST routes are very similar, though. The only difference is that you shall work with a Request object to get the post variables – then you must include the Request class from Symfony HttpFoundation. Like this:

If you don’t want to specify the method (GET, POST, PUT or DELETE) you can use the “match” method. This is useful for getting generic requests like you would do with the $_REQUEST global var in plain PHP. For example – if you want to match GET and POST for the same route and var:
Last, but not least: the order in which you define your routes is significant. The first route matched will be used – so place the more generic routes (like ‘/’ ) at the bottom.
For a complete reference about Silex Routes (there’s a lot of other options, including filters and requirements using regex for matching the request variables), take a look at the the documentation: http://silex.sensiolabs.org/doc/usage.html#routing

The purpose of this post was to give a brief introduction to Silex. If you liked what you saw, I strongly recommend you to read the official documentation, so you can take advantage of everything this microframework has to offer to increase your productivity when creating not-that-big projects. Here are some links for getting started:

Silex Website – http://silex.sensiolabs.org

Official Documentation: http://silex.sensiolabs.org/documentation

Matthias Noback’s blog about silex and symfony2 – useful posts and how to’s: http://php-and-symfony.matthiasnoback.nl/category/silex/