MVS Blog

Insights, Trends, and More: MVS Blog

Installing & Using the Composer

Installing & Using the Composer

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

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

Now Lets install it before getting how it works

Installing ComposerComp

If you are running on Windows, You should head to the Composer Website and its documentation because it's very handful for Windows users.

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

Open your terminal and give the command

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

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

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

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

Note:- This action may require your Sudo password.

Testing Composer

Now it's time to check this out, whether it's working… Again type in the terminal

$ composer

You can see this, it means everything is 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(sometimes you need to uncomment extension=phar.so in php.ini file). In other languages like Ruby have gem files for settings and packages, similarly here is composer.json. Now let's create a file named composer.json. In this file put

{

"require": {

"monolog/monolog": "*"</span>

}

}

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

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

$ composer install

It will automatically download the code and place it in the directory named vendor in your project's root. There is another Key too, for the selection of packages like minimum stability, etc.

Calling them in Project

Now as the composer downloaded content how it will called in our project? As a composer using PSR-0 autoloader(by the PHP Standards Group), it gives the ability to autoload any files you downloaded. It created autoloader.php automatically in the vendor directory. You simply need 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

From time to time you may be required to update your libraries and their dependencies or you just need to add more dependencies. The composer can do all these for you. You just need to update composer.json files and in the terminal type

$ composer update

. To update the composer itself you need to run the command

$ composer self-update

Closing Out

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

Top Clutch Development Company India 2022 Award
Top Web Development Company 2022
Image
MVS ISO/IEC 27001 Information security management systems Certification
MVS Microsoft Certified: Power BI Data Analyst