Slimming down: An introduction to Slim

January 14, 2016

It’s January and we’re all regretting how much food we ate at Christmas; our belts no longer fit and our favourite t-shirts are leaving little to the imagination. While we strive to remove some of our own excess holiday weight in January, is it possible to trim some of the fat off our applications too?

With the Slim PHP framework, our applications will only do what they are required to do. There’s little to no bloat, so long as you define your dependencies appropriately. Slim’s purpose is to provide a simple, yet powerful dispatcher that accepts an HTTP request and returns an HTTP response. Everything else, is up to us.

# Getting started

Installation

Open up your favourite terminal application, and type the following composer command to install Slim 3.x

Bootstrapping

To get Slim up and running, all we have to do is require Composer’s autoloader, instantiate the App() class and call it’s run() method.

At this point, we have a working application (albeit doesn’t particularly do anything). Try typing the following command in your terminal and go to “http://localhost:4000” in your browser:

If you see a 404 page in your browser, then it’s working. Slim has received your HTTP request and is now sending you back a 404 HTTP response. You are receiving a 404 response code because you have not defined any routes yet.

# Defining routes

In order to receive a particular HTTP response other than 404, such as a 200, you have to define routes within your application.

By adding the route definition above, we should be able to refresh the browser and see “Welcome to the application”. At this stage, we have a created a modern PHP application.

It still doesn’t really do much, does it? No, and that is where it’s true beauty lies. So rather than enforcing a particular structure or installing lots of (potentially) unnecessary features, Slim expects you to define the application’s dependencies yourself.

# Defining dependencies with Composer

Let’s pretend our application is going to read files from our filesystem. We may wish to use a nice wrapper class to read and write files rather than using the built-in PHP functions. Let’s require the Flysystem package:

Now that we have required the Flysystem package, our application now depends on it. We can now use Flysystem’s classes within our application:

Great Scott! If you refresh your browser you should now be able to read the contents of your file. Truly remarkable stuff.

One problem with that though - We really don’t want to (and shouldn’t) be instantiating the Flysystem over and over again on each route definition, so what can we do about that?

# The dependency container

Slim comes with it’s own light-weight dependency injection container. A dependency injection container (or Service container) allows us to centralize the way objects are constructed within our application. For example, to fix the little problem we had above, all we have to do is register the Flysystem’s classes as a service within our container.

We now have a clean, bloat-free application. Okay, it doesn’t actually do very much but the beauty of our application is that it only does what it needs to do.

We get so used to our shiny frameworks at times that we often don’t stop to think about how many of the features we actually use. It’s time to trim the fat.