composer

composer

Composer is a dependency manager for php. It also automatically generates all code necessary for autoloading.

The setup is simple:

place a composer.json in your project folder

{
  "name": "username/repo",
  "type": "framework",
  "license": "MIT",
  "description": "A project which gets things done",
  "keywords": [
    "project keywords", "another keyword"
  ],
  "authors": [
    {
      "name": "Florian Moser",
      "email": "me@myurl.ch",
      "homepage": "http://myurl.ch",
      "role": "Developer"
    }
  ],
  "require": {
    "php": ">=7.0",
    "phpmailer/phpmailer": "5.2.*",
    "famoser/minify": "dev-master"
  },
  "autoload": {
    "psr-4": {
      "username\\myproject\\": "src/"
    }
  },
  "minimum-stability": "dev"
}

Specifiy all packages  you need in the require node.
In the autoload node, define the standard you’re using, your vendor & project prefix, and the folder the source is located.

Let composer do its magic

composer install

This will download / actualize all dependencies specified in the require node (if applicable). Those files will be placed in a vendor folder. Inside this folder, there is a file named autoload.php. This file contains code to autoload all dependecies and additional sources specified in the autoload node. So all you need to do to use the dependencies is to include this autoload.php in your project.

Some other useful commands

composer update

When first running composer install, composer generates a composer.lock file, to ensure everyone executing composer install installs the same versions of the dependencies. composer update deletes this .lock file, and resolves the dependencies again.

You can download the tool from here.

Leave a Reply

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