Why Should Use Composer and How to Use It

One of the massive changes happened in PHP world is the Composer. You may be heard about composer but I don’t know either you use it or not. I am here to describe you why should we use composer, how much it is good for you and how to use it. Cheers!

What is Composer?

Composer is dependency management in PHP and it deals with various types of packages or libraries of PHP. Composer allows us to declare/write required PHP libraries/packages  for the project and it will install of those libraries/packages inside your project. And most important it will autoload all the installed  libraries/packages so that you can easily access these libraries/packages classes.

Why Should Use Composer?

We are using 3’rd party libraries/packages in our project. When we need a library then we download that library and store inside somewhere of our project. After that we require/include that library for access it’s classes. And we are doing same thing again and again when we need a another new library. That’s really annoying to a programmer or developer to download and include again and again, isn’t it? OK Composer comes to avoid this problem and provide a flexible way to manage all the packages or libraries inside your project. Together with Composer we have some rules and regulation, creating a PHP project with the same structure, using namespaces, the same code style, etc. For more information visit the composer official website https://getcomposer.org.

How to Use Composer?

Using of composer is really easy. Before you use Composer in your project your need to install Composer in your computer. By the end of this article, you will be able to plug and play with chunks of code in any framework, whether you work with CodeIgniter, FuelPHP, Laravel, Symfony2,Lithium, Yii, Zend… or anything else.

 

Installing Composer:

Installation of composer into you machine is really easy. Here I am showing the installation of composer for Ubuntu (Linux) operating system. If you are using Windows then see here.  Open you terminal and run this commands.

 

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

 

Setup Project With Composer:

To start using Composer in your project, you need to create composer.json file in your project’s root directory. And write the dependencies of your project (may also contain other metadata as well) in this file. The JSON format is pretty easy to write. It allows you to define nested structures.

With the ‘require’ keyword you can telling Composer which packages you will need to this project. And Composer will download all the packages from Packagist.

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

 

Here you can see I have required ‘monolog/monolog’ library with the version ‘1.0’ by using ‘require keyword. Here ‘monolog/monolog’ is the package name and ‘1.0’ is the version of this library.  Go to Packagist and you will get lot’s of php library you wanna need. Packagist is a repository Composer packages. Here you can find packages and lets Composer know where to get the code from.

Now just run ‘Install‘ command to get all the packages in your local project.

 

php composer.phar install

 

After run this command composer will find the ‘monolog/monolog’ library in Packagist and download the latest version in store into ‘vendor’ directory. And you will see a composer.lock file is create in your directory. Now go to created ‘vendor’ directory here you will see a ‘autoload.php’ file as well. You just need to require/include this class to using ‘monolog/monolog’.

 

require 'vendor/autoload.php';

 

Now you can easily access the ‘monolog/monolog’ classes in your project. Here I am writing a sample code so you can get some idea.

$log = new MonologLogger('name');

$log->pushHandler(new MonologHandlerStreamHandler('LOG_FILE_PATH/error.log', MonologLogger::WARNING));

$log->addWarning('Foo');

So these are very basic way to manage your project via Composer. To know the more details visit the composer official website https://getcomposer.org. I hope now you enjoy this tutorial and quite able to use composer. Best of luck for using Composer.