The Blog / Development / Slim Composed

Slim Composed

20 Apr 2012

Slim Framework has been around for some time now and it has become my de-facto micro framework for developing small projects or even just prototyping concepts easily. I have been long looking for a good package manager for PHP that would allow me to easily load any package I want to use in my projects.

I tend to use some of the same packages in most of my Slim related projects and I wanted a way to load these dependencies easily and be able to use them from the word go. I found Composer and thought this is the ideal tool to fix my current issue, as developers we like to try and keep everything DRY. Composer alone did not give the full DRYness that I was after but soon after I found out about Packagist which is like a central repository that keeps track of your packages, your packages are not store on Packagist only a reference is stored.

I have been in constant contact with Josh (Slim Framework lead developer) about getting this added to the Slim Framework project and he was delighted to have this added in, Slim was already PSR-0 compatible, this made using Composer autoloading class a breeze. Now Slim Framework is officially available from Packagist using Composer.

How to install Composer

Firstly we need to get Composer setup in our project root directory. You can do this by downloading the composer.phar directly from their website or just using this command in your Terminal:

curl -s http://getcomposer.org/installer | php

You should now have a composer.phar file downloaded inside your project root directory.

Installing Slim with Composer

Once you have Composer setup, you will need to create a composer.json file in your project root directory. In this file you will indicate which packages you want to add to your project. This is what mine looks like:

{
    "require": {
        "twig/twig": "1.6.*",
        "twig/extensions": "dev-master",
        "slim/slim": "1.6.*",
        "slim/extras": "1.0.*"
    }
}

As you can see I am telling Composer to download the following packages, twig 1.6.*, twig extensions, slim 1.6.* and slim extras 1.0.*. You might be wondering what the * stands for, this is just a wildcard to always get the latest minor version of that package. Check out the Composer website for more information.

Once you have your composer.json file ready, open Terminal and browse to your project root directory and type the following:

php composer.phar install

This command tells Composer to install the packages you have in your composer.json file into the default vendor directory which Composer will create.

Setup Slim

Now that we have Slim Framework downloaded along with all our packages that we will be using, we have to make some manual modifications to get our Slim setup working. Composer created a directory called vendor inside of your project root directory, inside you will find all your packages that were installed, copy the index.php and .htaccess from slim/slim directory and paste inside your project root directory.

Once you have your Slim index.php inside your project root directory you will need to add the following line:

require 'vendor/autoload.php';

and you can now remove:

require 'Slim/Slim.php';

We removed the manual loading of Slim and replaced it with Composer autoloader, which will load all the packages you installed along with Slim.

You Slim setup is now officially Composed.

Note

In my composer.json file above, I did not need to include the slim since I have slim extras in the list because the extras is setup so that composer will install slim because its a dependency for the extras.

Thanks to Josh for allowing me to add this and helping out with this awesome project.

Social Media

Twitter
London Midland Train Train Power Plug Square Bob Sponge Pants at o2 Arena