Quantcast
Channel: Eureka! » PHP
Viewing all articles
Browse latest Browse all 16

Composer – Manage your PHP dependencies

$
0
0

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


Filed under: PHP Tagged: Composer, Java, JSON, Linux, Mac, Maven, PHP, Windows

Viewing all articles
Browse latest Browse all 16

Trending Articles