Controller routes in Silex

I’ve been using Laravel 4 for a while now, and occasionally Kohana too, but in my current project I’m trying out Silex.  It’s a micro-framework, which means it only gives you the bare-bones rather than bucketloads of features, but that keeps it lightweight and gives the developer a sense that they’ve actually achieved something rather than just plugging pre-made boxes together!

One of the features I was quite comfortable and familiar with was the MVC approach of having routes and controllers.  Silex, being a micro-framework intended primarily to allow an entire app to exist in one index.php file, didn’t at first seem able to do this.  However, after a little hunting around I found it was possible, and I’d like to share it here for my own reference as much as anyone else’s.

Starting point

For reference, I’m using Silex 1.2 on PHP 5.4, and I have Composer installed on my test environment.  YMMV.

Let’s assume the following directory structure:

  • root
    • public
      • index.php
    • vendor
      • autoload.php
    • composer.json

All of Silex and the Symfony libraries are in vendor, and our app currently exists just in index.php.  For the sake of demonstration we’ll use some very basic code that looks like this:

In simple terms, this sets up Silex, sets two routes, and runs.  Simple enough.  But the functionality and their associated routes are all in closures, and for a larger app this could easily get out of control.

Implementing ControllerProviderInterface

Silex includes a nice interface called  ControllerProviderInterface  which can help us out.  It requires the following to exist:

Basically, unless you have that  connect()  function and return the right thing, your class doesn’t implement the interface properly.  That’s not a bad thing though, because the  connect()  function is where all the magic happens.


First, let’s set up a namespace so we don’t run into other people’s code.  Open up composer.json and add your namespace:

I’m calling our namespace “MyApp” for the sake of demonstration.  Save the file and update with Composer:


Next, we need to create the class file we’ve just referenced.  It’s important that what we specified in composer.json actually matches our files, so pay attention, otherwise you’ll get files not being found at runtime.

We’ve told composer that our MyApp namespace can be found in /src/MyApp, so that’s where we create our class file, for demonstration purposes called MyClassController.php.

  • root
    • public
      • index.php
    • vendor
      • autoload.php
    • src
      • MyApp
        • MyClassController.php
    • composer.json

The directory needs to match what we specified in composer.json, and it’s good practice to give it the same name as our actual namespace too.

Now we can create our MyClassController.php class:

It doesn’t do anything yet, but we’ll flesh it out in a moment.  What we’ve done here is specified our namespace, made sure we can access the right components, and implemented the interface.  Now comes the fun part.

Defining routes

We’ve created a function in our class called  home() , and told the controller to route GET requests to that function.  Make sure you correctly specify the namespace and class name too.  I won’t bore you with the details of why this works (read the documentation for that), but it does.  At least, it will do once we update our index.php file to use it properly…


So instead of defining our routes in index.php, we now let the class handle the routes itself, keeping index.php clean and succinct and allowing for more complex functionality to be extracted into separate files as needed.


Putting all this together, we have the following complete files:

Now (hopefully), we’ve got exactly the same functionality we had at the start, except that it’s much more organised and arguably more powerful.


2 thoughts on “Controller routes in Silex

Leave a Reply

Your email address will not be published. Required fields are marked *