Using Composer To Stop Reinventing The Wheel

/ PHP / by Paul Robinson / 0 comments
This post was published back on February 28, 2015 and may be outdated. Please use caution when following older tutorials or using older code. After reading be sure to check for newer procedures or updates to code.

Let’s say you are developing a cool web application in PHP. Maybe it’s a cool, but very simple gallery. You build out your album system, then suddenly you hit a seemingly small issue. You need slugs for the URL. Google hates generic IDs, so better use a slug derived from the album title. Fine, let’s just do a str_replace(' ', '-', $string);. Well, that’ll kinda work. Until you hit a special character that breaks your URL. You could use regex, but I’d hate to build that pattern if it needs to deal with international characters…

Composer to the Rescue

Luckily some kind soul out there has already done all this hard work for us and built a small library to make URL slugs from strings. In this case that library is Slugify.

In this ‘new to Composer’ tutorial I’m assuming you have never used composer before. If you have, and you’ve worked with Laravel, or any other PHP Framework that uses Composer you are unlikely to need to follow this tutorial, but feel free to read on if you’d like.

First you need to figure out if you already have Composer installed. If you are developing with something like XAMPP, then you are unlikely to have it installed. If you are using a VM like Vagrant, you may already have it. Please run composer --version at a terminal, if you get a version number you’re good to go. If not then you’ll need to install it.

On Windows, just head over to Composer and run the installer. You should be good to go straight away. If you are using a VM or Linux you will need to follow the install instructions to wget/curl a shell script to install it. Don’t worry, it’s super simple and the instructions are on the Composer website. I would advise following the global section if you’d like to be able to call composer with just composer as opposed to php composer.phar.

Composing Dependencies

Once you are done you will need to fire up a Console or a terminal and navigate to your web applications root directory. Composer is a command line tool, but don’t be scared it is very simple to use and saves you a lot of time once you know how to use it.

Let’s take two examples of how to include the Slugify library into our project. First is just the good ol’ command line. Just do the following:

This installs that single library and generates a composer.json file for us. The composer.json file is used to list the dependencies (the libraries you have asked for). Composer reads that file to make sure you have all of the libraries you have asked for.

If you want to list a few dependencies and have Composer install them all in one go, you can. Here we are only going to install Slugify and Intervention. Intervention is an awesome image manipulation library and would be very handy in our Gallery web app example. Here is how to do it:

You can use whichever way you feel is comfortable, however if you need to install a lot of dependencies when starting a project it can be a lot easier using the second method.

Using Composer Dependencies

It’s super simple to use the dependencies in your project, all you need to do is load the autoload file Composer has generated for you. This file will autoload any of the libraries you have installed when you need them, it’s just that awesome. Here is how you do it.

This is just a quick, very dirty example. You can of course use the use keyword so you can use the shorthand for making a slug, I used the long form simply for the example.


I love Composer. It has saved me a lot of time. It’s great for small PHP projects. Great for if your client only needs a micro framework, but needs some complex features like complex DateTime functionality (Carbon is awesome). You can even pull things into a WordPress theme if you find something WordPress doesn’t already have functions for.

It saves you time and effort. I say if someone has already wrote an awesome library & been kind enough to share it. Why not use it.

I thought I’d try out this style of tutorial with animated images (gfys). Do you like it? Or would you prefer the written tutorials? Would you like to see these used for hard to explain or complicated sections such as console commands, but prefer to see written code elsewhere (for you copypasta monsters out there)? Let me know what you think in the comments below.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

I'll keep your WordPress site up-to-date and working to its best.

Find out more