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.

Comments (1)

  1. Very helpful – thank you

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

Avoid to use ENUM and SET type in MySQL

Posted on 12 years ago

Bhumi

MySQL coalesce() function

Posted on 12 years ago

Bhumi

How To use Pagination in Joomla

Posted on 12 years ago

Bhumi