Composer Basics

About Composer

Composer is a tool to manage dependencies in your project. With it you can search, install and remove external packages in your code just in few commands. It tracks information about updates and dependencies using centralized repositories (the largest is packagist.org).

Installing

You can install Composer just in two commands:

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
Using

Create composer.json file in root folder of your project. It will contain information about your project like: author, version, dependencies, install scripts, namespaces, etc. Check out sample:

{
   "name": "project/package",
   "require": {
      "php": ">=5.4.0",
      "swiftmailer/swiftmailer": "5.3.0"
   }
}

In this example project has called project/package where project is the large project name, package is package inside large project, e.g.

"zendframework/zend-diactoros":"*",
"zendframework/zend-dom": "*",

If the project consists of single package their name usually are same, e.g

swiftmailer/swiftmailer

After package name you can see requirements block. There you can specify required parameters and dependent packages like:

Version of PHP
"php": ">=5.4.0"
PHP extension
"ext-imagick": "*"
Package
"swiftmailer/swiftmailer": "5.3.0"

If you will publish your code and someone will try to install your code on the system which doesn’t fit the requirements error message will be displayed, for example:

Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

Problem 1
- The requested PHP extension ext-imagick * is missing from your system.
Installing packages

After you have specified required packages you can install them with the command:

composer install

If you want to update already installed packages use

composer update

After that vendor directory will be created in your project root. The directory will contain external packages, e.g.:

vlad@ubuntu:/var/www/project$ ls -la vendor/
total 188
drwxrwxr-x 46 vlad vlad 4096 Jan 27 08:35 .
drwxrwxrwx 11 www-data www-data 4096 Jan 29 08:17 ..
-rwxrwxr-x 1 vlad vlad 178 Jan 27 08:35 autoload.php
drwxrwxr-x 3 vlad vlad 4096 Jan 27 08:35 barryvdh
drwxrwxr-x 2 vlad vlad 4096 Jan 27 08:35 bin
drwxrwxr-x 3 vlad vlad 4096 Jan 4 08:24 classpreloader
drwxrwxr-x 2 vlad vlad 4096 Jan 27 08:35 composer
....

Now you can use all of them in your code. To do that:

1. Add 

require __DIR__.'./vendor/autoload.php';

line at the beginning of your index.php file (of another one where you want to use packages).

2. Add necessary packages in use section if your script e.g. 

<?php
require __DIR__.'./vendor/autoload.php';
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

Note! PSR standard is strongly recommended for packages, but not all of them correspond it (mostly legacy packages).

3. This is it!

Conclusion

Using of PSR and Composer in your projects will allow you to use tons of external packages and share your code with colleagues in an extremely simple way. Check out my notes to know more about it!