Autoload files and manage package using composer.

@mit $ingh. September 30, 2016 Comments

composer.json

{ 
    "name": "corephp",
    "description": "corephp.",
    "keywords": ["framework", "corephp"],
    "license": "MIT",
    "type": "project",
    "require": {
        "phpmailer/phpmailer": "^5.2"
    },
    "autoload": {
        "classmap": [
            "App/ClassFiles"
        ],
        "psr-4": {
            "App\\": "App/",
            "Model\\": "App/Model/",
            "Controller\\": "App/Controller/",
            "Vendor\\": "App/Vendor"
        },
        "files": [
            "config/constants.php",
            "App/CommonFunction/CommonFunction.php"
        ]
    }
}

Execute below command to download phpmailer and automatically autoload all the class files.

As written in the composer.json file of phpmailer.

composer require phpmailer/phpmailer

Note : If you are not passing version no. than it download the latest version.


Execute below command to autoload different files in different types.

composer dumpautoload

1. classmap

"classmap": [
            "App/ClassFiles"
        ]

It autoload all the classes(which don't have namespaces) Inside App/ClassFiles. 
To confirm you can check inside "vendor/composer/autoload_classmap.php". Here
it includes all the classes.

 

2. psr-4

"psr-4": {
            "App\\": "App/",
            "Model\\": "App/Model/",
            "Controller\\": "App/Controller/",
            "Vendor\\": "App/Vendor"
        }

It autoload all the classes(which have namespaces).
"App\\" -> It means namespace start with App\Dir1\Dir2;
"App/" -> It is the path of the class files from the parallel of composer.json file.
To confirm you can check inside "vendor/composer/autoload_psr4.php". Here
it includes all the classes.

3. files

"files": [
            "config/constants.php",
            "App/CommonFunction/CommonFunction.php"
        ]

It autoload all the normal files as path given from the parallel of composer.json file.
To confirm you can check inside "vendor/composer/autoload_files.php". Here
it includes all the files.

  • autoload
  • composer-phar
  • composer
  • composer.json