Getting started with PHP dependency manager Composer


Hi Guys, my last article was about “Creating custom Jquery Plugin”. Today I would like to introduce you with another best project in PHP world, Composer. Today’s PHP is not like grandfather PHP, it is completely different, much more matured, elegant and ready for enterprise development. So the question is why Composer? How to manage package with composer?

Yes, composer is the great tool to manage your project dependencies, keep you updated with the dependencies. It get you the package needed for your current version and also download, manage if that package use other packages. Most of the modern frameworks are Composer powered, few examples – Cygnite PHP Framework , Zend , Symfony etc. So you are having freedom to use any third party library into your framework.

The main feature is package can be written such a way that can be usable or pluggable into any other application supported by composer.

Step1: Installing Composer

Though there is a beautiful documentation in the composer website , if you are looking for short installation and usage tutorial you can start looking at steps below.


curl -s | php

Above command will download composer.phar into your current location.

To make composer globally accessible.

sudo mv composer.phar /usr/bin/composer

This command will help you to move composer.phar into bin directory and giving you access to all composer commands with much shorter syntax.

For example:

composer.phar --version

Can be run as

composer –version

If you are Windows user you may download and run composer.exe.

Step2: Using composer.json

If you aware about the ruby (gem) or node.js (package.json) then composer.json may be familiar to you. It mostly inspired by ruby bundler and package.json. Composer uses json file to provide settings and package information for your awesome application.

Create a file into your root directory called composer.json and paste the below lines.

    "require": { 
        "cygnite/framework": "1.*"

Above example is for basic composer settings. The require keyword used for providing list of dependencies of your project. You can see in this example dependency is Cygnite Framework version 1. Cygnite Framework dependent on very few packages composer will install those automatically when you run below command into command line interface in the directory where your composer.json file exits.

 php composer.phar install
 composer install 

You can see composer is downloading Cygnite Framework and its dependencies one by one. Interesting right!

You can also see composer.lock file into your directory which is used to track all version of packages currently installed. If you change anything in composer.json file you may run below command to keep you up to date.

 composer update


Now go to the project directory and you can see vendor folder where composer installed packages. Composer also follows PSR-0 autoloader to autoload all your dependencies. You can see file called vendor/autoload.php

You can simply use autoload.php into your project and start using the package.


 require_once "vendor/autoload.php";

Step3: Sample Usage of Autoloader- Using Cygnite Asset Manager

Step2 we have installed all packages using composer, now let me show you how you can make use of those composer packages into your project.

Create a index.php file and paste below code and make sure assets exists into your application directory. Sample usage below.


require_once "vendor/autoload.php";


use Cygnite\AssetManager\AssetCollection;
use Cygnite\AssetManager\Asset;

$asset =  AssetCollection::make(function($asset)
    $asset->add('style', array('path' => 'composer-tutorial/assets/twitter/bootstrap/css/bootstrap-theme.min.css'))
                ->add('style', array('path' => 'composer-tutorial/assets/twitter/bootstrap/css/bootstrap.min.css', 'media' => '', 'title' => ''))
                ->add('style', array('path' => 'composer-tutorial/assets/css/stylesheets/*'))
                ->add('script', array('path' => 'composer-tutorial/assets/js/cygnite/jquery.js'))
                ->add('script', array('path' => 'composer-tutorial/assets/js/custom.js'))
                ->add('script', array('path' => 'composer-tutorial/assets/twitter/bootstrap/js/bootstrap.js'));

    return $asset;



You would have heard about the composer but haven’t had time to check. So I hope you have learned something today. Composer is very useful Dependency Management tool in PHP world. If I missed anything above you may also have a look at Composer official documentation.

If you find above tutorial helpful, don’t forget to give a small like or share with friends and also leave your comments below. Keep visiting.

No comments:

Post a Comment

Follow Us On Facebook Open Source Web Developers by Appsntech facebook group Twitter Open Source Web Developers by Appsntech twitter group Google+ Open Source Web Developers by Appsntech Google group Linkedin Open Source Web Developers by Appsntech, LinkedIn group
Copyright @2011-2015 All rights reserved. Powered By- Sanjoy Dey Productions