×

Thank You for Quotation!

Your email has been successfully sent and we will get in touch with you shortly.

Request a Quote

Please tell us about your project and a member of our team will contact you within one business day.

Managing the dependencies in PHP web applications was a well known headache. Its due to lack of its own Package manager. Other languages have already their Dependency Manager like Ruby have Bundler, Maven for Java, NPM for Node.js and so on…..

Nowadays functionality of web applications gets more robust and challenging, you need to pick different bits from all over Internet. It will be very difficult to manage it in our web application. You will get code library and this library may have their dependency and they should have more dependencies.

Here Composer come to action. It will take care of all dependencies in the package. No matter you are using any framework like CodeIgniter, CakePhp, Zend, YII or my favorite Laravel. It will pull all required libraries and put them at appropriate places(Generally in Vendor Directory). Most important thing about Composer is, it will also take care of updating of those libraries with just only one command.

Now Lets install it before getting how it works

Installing Composer

If you are running on Windows, You should head to Composer Website and its documentation because its very handfull for Windows users.

If you are on Mac or any Unix based system, you should go as follows-

Open your terminal and give command

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

I would recommend to use MAMP PHP globally instead of system default(/usr/bin/php) PHP

Now as we want use this composer globally thought system, we need to move it bin directory.

$ sudo mv composer.phar /usr/bin/composer

Note:- This action may require your Sudo password.

Testing Composer

Now its time to check this out, whether its working… Again type in terminal

$ composer

You can see this, it mean everything correct and working. You can also check the version of the composer you have by typing

$ composer --version

Note:- If you installed composer locally then you need to run $ php composer.phar instead of $ composer

Understand Working

Composer contains an executable Phar archive(sometime you need to uncomment extension=phar.so in php.ini file). In other languages like Ruby have gemfile for settings and packages, similarly here is composer.json. Now lets create a file named with composer.json. In this file put

{
"require": {
"monolog/monolog": "*"
}
}

Here we are loading common logging librery Monolog. See the " * " in it, it mean we are telling composer to install most updated version(suitable to your installed PHP), you can replace " * " to its version as "1.10.0".

Now its time to see the action, go to the terminal(Make sure you are in your project Directory) and type

$ composer install

It will aromatically downloads the code and placed in the directory named vendor in your project's root. There is other Key too, for selection of packages like minimum-stability etc.

Calling them in Project

Now as composer downloaded contents but how it will called in our project? As composer using PSR-0 autoloader(by the PHP Standards Group), it gives ability to autoload any files you downloaded. It created autoloader.php atomatically in vendordirectory. You simply needs to put a single line in your index or bootstrap file as

include_once '/vendor/autoload.php';

Now you can start these libraries without hassle.

Keep Updated

Time to time you may required to update your libraries and its dependencies or you just need to add more dependencies. Composer can do all these for you. You just need to update composer.json files and in terminal type

$ composer update

. For updating composer itself you need to run command

$ composer selfupdate

Closing Out

As soon you going to use composer, you will feel how handy it is, Package Management is very important aspect in current PHP programming and Composer handles it very professionally. Make sure to go through with its Official Documentation for technology updates and new features.

Comments