What is Composer in PHP?
Composer is the dependency manager of PHP. It downloads the libraries (called packages) that your project needs, and keeps them updated. Packages come from a website called Packagist.
You write the needed packages in a file named composer.json. Composer installs them in the vendor folder and also creates an autoloader, so you do not need to write require for every class.
| Command | What it does |
|---|---|
composer init | Creates a composer.json file |
composer require vendor/package | Installs a package and adds it to composer.json |
composer install | Installs all packages listed in composer.lock |
composer update | Updates packages to the latest allowed version |
composer dump-autoload | Rebuilds the autoloader |
<?php
// One line loads every installed package
require __DIR__ . "/vendor/autoload.php";
?>
Composer is used by almost all modern PHP frameworks like Laravel and Symfony. Do not upload the vendor folder to Git, only composer.json and composer.lock.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |