How to use composer autoloader

# Use Cases
use psr4
autoload a class file or a directory of classes


# Solutions

# Manually
$loader = require YOUR_VENDOR_PATH'/autoload.php';

$loader->addPsr4('projectName\\ClassDir\\', "path/to/classes/");
// inside classe folder there are classes with namespace projectName/ClassDir;


$classmap = array(
    "ClassName" => "path/to/classes/ClassName.php",
    "ClassesDir" => "path/to/directory/",
);
$loader->addClassMap($classmap);


# Automatic
# Using composer.json to auto generate autoloader settings
{
  "autoload": {
    "psr-4": {
      "projectName\\ClassDir\":"path/to/classes/"
    },
    "classmap": [
      "path/to/classes/ClassName.php",
      "path/to/directory"
    ]
  }
}

# Update the Composer autoloader
composer dumpautoload -o


# References
http://phpenthusiast.com/blog/how-to-autoload-with-composer
Free Tags: