Composer easy tutorial – php dependency management tool

What is composer ?

Composer is a tool for dependency management for your php projects.

Why we’ll use it ?

Suppose you are working in a project which has lots of dependency on others project or libraries.

Composer will manage that dependency on below ways.

  1. Download all the dependency library from their repository to your project automatically.
  2. You can easily update your library when a new version of that library comes out.
  3. When downloading dependency library composer also checks minimum server requirement.
  4. Composer will create a single autoloader.php file for all downloaded library and autoload all the dependency into your project.

If I don’t use composer then?

  1. You need to download all the dependent library manually.
  2. Before use in your project, check server requirement manually.
  3. You have to check a new version of the libraries comes out or not, if yes then have to again download manually in your project.
  4. You need to load all the libraries using require or include in your project .

I’m not getting you, give me an example?

Okay, you have a project on cakephp or laravel. You want some mail functionality, like SMTP connection So you will download a library like Phpmailer or Swiftmailer.

If you download this library through composer you can directly download into your vendor folder. If this library updated, you can easily update your package with a single command, no need to check that, this version will run on php 5.4 or 5.3

Is it make sense? if not then don’t worry I will give some practical example.

How do I install Composer in my system?

Before installing composer make sure, your php has at least version 5.4

Okay If you are windows user just grab .exe file from https://getcomposer.org/ , install composer in same directory where your php.exe installed like “C:\wamp\bin\php\php5.5.12”

If you are linux or mac user, then open your terminal and paste below command.

curl -sS https://getcomposer.org/installer | php

It will download a composer.phar (phar ~ php archive) through curl. Access composer globally on your system you need to move your composer.phar file.

sudo mv composer.phar /usr/bin/composer

That’s all.

How do I check composer is installed or not?

Open your Command prompt or Terminal write “Composer” and hit enter. It will show a nice welcome screen and all command, usage of composer.

composer

Lets have a practical example.

In your project directory, Composer will look for a composer.json file where we’ll add all dependency.
lets create a directory.

mkdir composer_example

then cd into the directory.

cd composer_example

composer

Now we’ll create a composer.json file here.

{
    "require": {
          "jdorn/sql-formatter": "1.3.*@dev"
    }
}

Here “jdorn” is vendor name and “sql-formatter” package name.

Hey, 1 sec where can I get “jdorn/sql-formatter” ?

Composer has an another site https://packagist.org/ where you can browse your package. All popular libraries are already there.

packagist1

We want to add “sql-formatter” library into our project folder. “sql-formatter” is a lightweight php class for formatting sql statements. Handles automatic indentation and syntax highlighting.

packagist2

Now tell composer to download / install required files for this package into our project directory.

composer install

composer

I will download all required files including an autoload files in “composer_example/vendor” directory.

Now create a file index.php on composer_example and include autoload.php , all dependencies will be loaded by this file. here is working example that how can we use “sql-formatter”.

<?php

require "vendor/autoload.php";

$query = "SELECT count(*),`Column1`,`Testing`, `Testing Three` FROM `Table1`
    WHERE Column1 = 'testing' AND ( (`Column2` = `Column3` OR Column4 >= NOW()) )
    GROUP BY Column1 ORDER BY Column3 DESC LIMIT 5,10";

echo SqlFormatter::format($query);
?>

composer

So you can see how quickly and easy way we can manage dependency.

Let’s create a project.

Suppose we want to add codeigniter through composer. Then we’ll not use “composer install”, instead we’ll use “composer create-project”.

So browse “codeigniter” in https://packagist.org/

Now grab a copy of the framework into “your_codeigniter_project” directory.

composer create-project rogeriopradoj/codeigniter your_codeigniter_project

composer5

In www/your_codeigniter_project directory, you’ll find a composer.json file.You can add more dependency on this file.Suppose we want add “sql-formatter” in this codeigniter project edit the composer.json file in below way.

{
    "description" : "A way to install CodeIgniter via composer",
    "name" : "rogeriopradoj/codeigniter",
    "license": "OSL-3.0",
    "require": {
        "php": ">=5.2.4",
        "jdorn/sql-formatter": "1.3.*@dev"
    }
}

Now update the dependency using

composer update

composer

Now you want submit this project github or send it to your friend no need to send vendor folder, just submit composer.json file, using composer install your friend can easily grab all dependency that has been used in this project.

Let’s give it a try, All major php projects like Laravel, Symfony 2, Yii or some extraordinary php packages from Phpleague are already using heavily. What are you waiting for ?

  • Krysha Seane

    Im trying to update my composer getting error, 30 days old version, how can I solve it,

    composer update is not working.

  • Ayan Sil

    nice tutorial sir

  • Bijoy

    Just made a laravel project using composer,git.. its a first one for me.. now i m digging more into it. I think laravel is my next destination.. keep publishing
    ..