Composer is PHP's standard package manager — it installs and manages external libraries, and (just as usefully) handles loading a project's own classes automatically, without a manual require for every file.
composer require guzzlehttp/guzzleThis downloads the package into a vendor/ folder and records it (with its version) in a composer.json file, so the exact same dependencies can be reinstalled anywhere.
{
"require": {
"guzzlehttp/guzzle": "^7.0"
},
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
}One line, at the top of the entry file, loads every installed package's classes.
require 'vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();The "App\\": "app/" line in composer.json above tells Composer that any class in the App namespace lives in the app/ folder, following the same path — no manual require needed for a project's own files either.
// app/Models/User.php
namespace App\Models;
class User {
// ...
}
// anywhere else in the project, after 'vendor/autoload.php':
use App\Models\User;
$user = new User(); // Composer finds and loads the file automaticallyAfter adding or moving a class, running composer dump-autoload rebuilds the autoloader's internal class map.
composer dump-autoload| Command | What it does |
|---|---|
| composer init | Creates a new composer.json for a project |
| composer require | Installs a package and adds it to composer.json |
| composer install | Installs everything listed in composer.json — used when cloning a project |
| composer update | Updates packages to their latest allowed versions |
| composer dump-autoload | Rebuilds the autoloader after adding/moving classes |
Where packages come from
Packagist (packagist.org) is the official package registry Composer searches — worth checking there before writing something from scratch that a well-maintained package already solves.