PHP Composer : Manage Package Dependency in PHP

What’s a PHP composer? How to use PHP composer in your PHP Application? What are the benefits of using composer in your application?

PHP Composer is a dependency manager. It manages the dependency of PHP packages in your application. Using composer you can pull the required library and dependencies and manage them in one place. The concept of a composer is similar to npm in node.js and gem in Ruby.

Let’s first understand what is a dependency.

What’s a Dependency?

A dependency is an external library or a component which is required to run our web application.

For example – It might be OAuth2 library, facebook PHP-SDK etc.

Why PHP Composer

So you might be thinking, why I need to use composer for managing package dependency. What kind of problems composer is going to solve? A Composer will solve these three important problems.

  1. Dependency resolution for packages.
  2. Autoloading packages.
  3. Update all your existing packages

Let’s understand, what is dependency resolution.

Suppose you have downloaded any PHP framework or library. Now the framework you have downloaded depends on an external library such as guzzle and JSON-schema etc to work.  Similarly, guzzle needs Symfony’s event-dispatcher and the list goes on. Composer handles these dependency resolutions automatically.

How to Install PHP Composer

I assume you have installed PHP in your system. To download and install-composer, go to the terminal and type following commands.

If you are getting curl error then first install curl in your system.

Linux commands with examples

In next step, move composer.phar to the bin directory.  After moving this file to bin directory you can access composer globally on your system

Now installation is complete. You can use composer to manage and update package dependency. To check composer available commands, simply type composer in your terminal and hit enter.

It displays all the available composer commands.

How to Install PHP Composer on Windows

If you are looking installation guide for windows operating system, then check the installation manual on Composer Website.

How to Use Composer

We have installed composer successfully. Let’s pull some packages through composer.

First, create a composer.json file in the root of your project directory. Suppose, I want to download slim and twig packages for my project. I included the vendor name and version of slim and twig in a composer.json file.

After that run install command and composer will install the required dependency.

So now you are thinking from where you get the vendor name and version for composer.json file. You can get this information from Packagist .

How to Add Another Dependency on a Existing Composer File

To add a dependency on existing composer.json file, open this file and add another require statement with a dependency name. The other easiest approach is to run composer require command.

This command will pull a dependency and also it updates your composer.json file.
How to Update Packages

The composer update command updates all the packages mentioned in your composer.json file to a newer version.

Using Packagist to Define Dependency on Composer.json file

Packagist is the main repository. It aggregates all sorts of PHP packages that are installable with Composer.

To search the package, just type the package name. Once you click on a package , you’ll get the vendor name and version. Put the package vendor name and version in a composer.json file.

For example,  I am searching a PHP framework, In our result we get a list of a framework based on the no. of downloads and rating. Manage Package Dependency in PHP through Composer.json File After choosing the framework, it will tell you how to use require inside composer.json file. For installing laravel, i need to add “laravel/framework”: “4.2.*@dev” in a composer.json file with require.

Package Autoloading

Now the package/library you require is installed. You can check vendor directory.

To use them in your project composer-comes with Autoload file.

Once the autoload file is included, you can use the library in your project.

Conclusion

I hope this article helps you to understand, How to manage package dependency in PHP. Why we need dependency management tool. The main benefits of a dependency management tool is to provide consistency and ease of use.

Tagged , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.