Back to Top

A Beginner’s Guide To Composer

A Beginner’s Guide To Composer

A composer is a tool for dependency management in PHP. It basically allows you to declare your project dependent libraries and helps to manage dependencies.The composer is one of the most recommended tools which is used in all modern PHP frameworks and solves fundamental issues in the projects. A Guide To Composer explains all about the composer and it’s usage.

The Composer Dependency management tool is very helpful to easily set different useful things and facilitates the installation, upgrade, and support for various libraries in your project

This article will give you an introduction to Composer and how to manage it in PHP. It will also explain what you need to change in your deployment process after integrating composer.

What is a dependency manager and Why do I need one?

Sometimes you write the code which is dependent on third party libraries, For example, you are using PHPExcel or bootstrap etc in your application and you require to update the library with new version release. All the libraries are dependent on the project so Composer allows you to manage dependencies easily and also you can upgrade all the dependent libraries easily.

All the third party tools managed through packages or we can say every dependency is for a package. Package constitutes a local file or folder, remote file or folder, local or remote Git repository etc.Most of the developers use a global registry for available packages. Packagist is most widely used for Composer.

For third party PHP library and package, instead of manually download and add them to our code base, the composer should be used instead.

There is also a Packagist repository so that you can find php packages here.

How to install Composer?

Installing Composer is pretty simple just it requires PHP 5.3 or greater version. You can follow the instructions for either *nix or Windows.

More information about composer setup can be found here: Introduction – Composer

Install/Update package with composer

To install the composer, Run the following line in your terminal to get the latest Composer version:

The composer is a package manager that works according to the rules written in composer.json.

For example,

composer.json is the required file to use the composer. Composer.json is mainly used to get the information about what packages you’re using in your project. Here you can see require takes an object that maps package names and version.

How to Manually Download Composer?

If you prefer to download the phar manually, you can get the latest version here https://getcomposer.org/download/

Once the composer is installed into your local development area, run composer update to update all existing packages.This command will updates all installed (or reinstalled accidentally deleted) packages to the latest versions.

Packages will be stored in the vendor directory. We won’t track this via our repository, rather, we tracked the composer.json and composer.lock file.

To install or use new PHP lib/package, first you need to check if they are available on https://packagist.org/ and you just have to run composer require package-name to add new PHP package.

For example, find the PHPExcel package on https://packagist.org/ and then run

SYNTAX:

The best way to add a new dependency to the file composer.json is to use the following command.

This will add all the necessary dependencies to file.

If you need to add the packages to the section the require-dev , add the command option –dev :

Also, a require allows you to add multiple packages at the same time, just simply separate it by a space.

When you install new packages, the composer.json and composer.lock file will be updated. This will make sure all developers will be using the same third party packages.

More information can be found on the composer basic usage page here: https://getcomposer.org/doc/01-basic-usage.md

Load third party packages into code base to use

To load/include the third party package into the code base to use, instead of manually using PHP include to call for the library, composer provides an easy way to load the packages. Simply put:

at the top of the PHP file that you are going to use the third party lib.

When installing packages Composer iterates by their rules and generates the most important file – / the vendor / autoload.php ,

Composer solves the following problems:

  1. To solve the dependency of PHP packages
  2. Provide autoload solution for PHP packages
  3. Easily update all packages

There is a brief overview of features.There are only two package management and autoloading classes and without the composer, both of these tasks take a lot of time.

Share:
Share on Facebook0

Share on LinkedIn2Tweet about this on TwitterShare on Google+1Buffer this pageDigg thisShare on Reddit1

Leave a Reply

Your email address will not be published. Required fields are marked *

Most Popular Posts

How To use Pagination in Joomla

Posted on 5 years ago

Bhumi

Really! PHP touches the 200 Million Mark

Posted on 4 years ago

John Pitt

PHP 7 new Features at a Glance

Posted on 1 year ago

Bhumi

How does CI/CD works on AWS?

Updated 17 years ago

Bhumi

Overview of Amazon Web Services (AWS)

Updated 17 years ago

Bhumi

How to add ColorDots in FullCalender?

Updated 17 years ago

Bhumi

A Beginner’s Guide To Composer

Updated 17 years ago

Bhumi

The Reader’s Poll – June 2015

Posted on 1 year ago

Bhumi

The Reader’s Poll – August 2014

Posted on 2 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 2 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 2 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 3 years ago

Bhumi