Composer Basics

PHP is a pretty cool language, yeah. It’s super easy to create dynamic websites with a very small learning curve. Well, it’s easy until your site gets big. Or you need to move to a new computer. What if you lose a file and your entire app crashes? What if dependencies you use clash with each other and cause unforeseen errors well into production? How can we combat this? Enter Composer.

##Composer

Composer is the remedy for PHP’s nightmarish dependency management situation. Composer is a command-line tool that works similar to NPM. You can easily add or remove dependencies. Plus, if you host your code in version control, you can upload your code without having to include the dependencies themselves. Others can just composer install and have a working copy in minutes.

##Installation

###Mac/Linux: With CURL: > curl -sS https://getcomposer.org/installer | php

Otherwise: > php -r “readfile(‘https://getcomposer.org/installer’);” | php

To move Composer into your /bin directory and install it globally: > mv composer.phar /usr/local/bin/composer

###Windows

Nobody really likes us on Windows. We’re weird, and installing programs is unnecessarily complicated. Regardless, click this link to download the newest official Composer Windows installer. This basically just installs Composer to your AppData directory and lets you put the tool in your PATH.

##Usage

Now, we can easily use composer just by running this command in the shell: > composer

If you installed it locally only: > php composer.phar

You will mostly use 4 composer commands: install, update, require, remove and self-update. Let’s talk about each one a bit more in-depth.

###install

composer install

This command is almost identical to npm install. This command assumes there is a composer.json file present in the working directory (more on composer.json later). Composer will enumerate through the dependencies listed in composer.json and install them all to the /vendor directory. Composer is awesome in that it resolves all conflicts and installs the most suitable version of every dependency you need.

After this, all you need to do to have access to every single dependency in your app is include this code at the beginning of your main source file:

<?php

// Use an autoload.php path relative to your file.
// Here, I assumed that your *.php file is located in a directory that
// contains a subdirectory named vendor/.

require('vendor/autoload.php');

// Your code here...

###update

composer update

This command does exactly what you would imagine - it updates your project to work with new dependencies, and also updates all dependencies to the highest version available that will not create a conflict with any other dependency. This also assumes you have a composer.json file.

###require

composer require (vendor)/(package)

This command requires the given package by adding it to composer.json. It does not require a composer.json file. One will be made if not present.

###remove

composer remove (vendor)/(package)

This command removes the given package from composer.json and uninstalls it from vendor/.

###self-update

composer self-update

This command simply updates Composer itself.

###composer init and composer.json

composer init

This command begins a series of prompts that ask you questions about your project and generates a composer.json file in the working directory.

###composer.json

Similar to a package.json file, composer.json describes your project and lists its dependencies. Full documentation can be found here. Read it. Now.

##Conclusion The composer documentation can be found here. Thanks for reading this article. Good luck with your PHP development!