What is Composer?

Composer is a dependency manager for PHP. You might be thinking what the heck is a dependency manager? Is it some kinda corporate stuff? Well no, let me exerciselain it simply.

When you start learning php you will notice that if you work on larger scale some of the code can be re-used or some of the functionality can be re-used.

For example: user authentication, Request and Response management or Form Validation etc.. Rather then re-inventing a wheel or writing it from scratch developers like to re-use the code which has already been written by professional developers.

Now, question is how to we get such functionality from other developers? This is where composer comes in you can install composer on your local machine and get third party libraries in your php project.

How to install composer on Ubuntu?

To install composer you need to have it's dependencies on your ubuntu machine. Followings are dependencies of composer:

  • PHP
  • Curl

To install composer follow the steps below:

# install curl extension
sudo apt-get install curl

# install composer globally
curl -sS https://getcomposer.org/installer | \
sudo php -- --install-dir=/usr/local/bin --filename=composer

# check if it is installed
composer -V

How to configure composer?

To use composer with your php project first go to your php project directory and create a file called composer.json with following minimal configurations:

{
    "name": "vendor/package",
    "description": "Give some nice name for your project",
    "keywords": ["php", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {},
    "require-dev": {},
    "autoload": {
        "classmap": [ ],
        "psr-4": {},
        "files": []
    },
    "autoload-dev": {
        "psr-4": {}
    }
}

Let's understand the composer syntax:

  • name - Namespace for your package usually vendor/package_name
  • description - you can describe about your package
  • keywords - you can add keywords related to your package
  • licence - there can be different types of licencies depending on your project privac
  • type - you can define type of your package or library
  • require - all required php third party libraries
  • require-dev - all required php libraries for local use only
  • autoload - loads classes or files or namespaces automatically
  • autoload-dev - classes or files or namespaces to be loaded during local environment only

Once you have composer.json file you can now install the dependencies using following commands:

# To install packages from other developer
composer install

# To update or upgrade existing packages 
composer update

When running install command it will create a directory called vendor which will contain all third party libraries that we need for our project.

For example: Let say that I am creating a github project and I am using a namespace for my project I have following structure for my application.

project/
   |-- src
   |     |-- Model.php        -> App\Model
   |     |-- Core.php         -> App\Core
   |
   |-- vendor
   |     |-- autoload.php
   |
   |-- composer.json
   |-- index.php

Let's modify our composer.json file before we install our dependencies:

{
    "name": "vendor/package",
    "description": "Give some nice name for your project",
    "keywords": ["php", "laravel"],
    "license": "MIT",
    "type": "project",
    "autoload": {
        "psr-4": {
           "App\\": ["src/"]
        }
    }
}

Take a close look at above file in here we are telling composer that resolve any namespace starts with App and look them in src folder.

Now, when we have to run composer install command so that composer will generate a file called autoload.php in vendor folder.

Now, you have to include this autoload.php file in your index.php file so that when your application runs it will autoload classes from src directory with specified namespaces.

Open your index.php file and add following code:

<?php

# include autoload.php file here
require_once __DIR__. "/vendor/autoload.php";

# Now, composer will automatically resolve your
# class namespace yeah !!!
$class = new \App\Model();

That is it, composer does a lot more stuff that you don't believe. I will cover them in different course.