Composer – Manage your PHP dependencies

Mess up with the PHP dependencies in different environments? Composer is a PHP dependency manager where all the dependencies information are stored in the JSON file called composer.json. Similar to the pom.xml if you are using Maven in Java.

This example is done on a Windows machine. You could refer to Composer website if you are using Mac or Linux.

1. Download and install the Composer as stated in the Composer website.

2. Check if the Composer command works in shell.

composer --version

 

3. Create a composer.json in the following pattern inside your webroot folder.

{
  "name": "your-vendor-name/package-name",
  "require": {
    "php": ">=5.3.0",
    "vendor/package0": "1.*",
    "vendor/package1": "1.3.2",
    "vendor/package2": "1.*",
    "vendor/package3": ">=2.0.3"
  }
}

 

4. Here is an example of composer.json.

{
  "name": "eureka/test-composer",
  "require": {
    "php": ">=5.3.2",
    "guzzle/guzzle": "*"
  }
}

 

5. Open the shell and go to your webroot folder. Run

composer install

php-composer-1
 

6. Now check your webroot/vendor folder and you would find all the dependencies are downloaded.
php-composer-2
 

7. You could use the autoload.php in include all required classes.

<?php
  require 'vendor/autoload.php';

  // YOUR CODE
?>

 

Done =)

Reference: Composer – Dependency Manager for PHP

Advertisements

One thought on “Composer – Manage your PHP dependencies”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s