Composer is the go-to dependency manager for PHP. If youβre still copy-pasting libraries manually, itβs time to level up your workflow! πͺ
π― In this guide, youβll learn:
β
What Composer is and why you should use it
β
How to install and configure Composer
β
How to install and autoload PHP packages
β
How to create your own Composer package
β
A real-world mini project using Composer
Letβs dive in! π
1οΈβ£ What is Composer?
π‘ Composer is a dependency manager for PHP that automatically installs, updates, and loads packages (libraries) for your project.
π₯ Why use Composer?
β
Manages dependencies easily
β
Prevents compatibility issues
β
Autoloads classes (No more require
headaches!)
β
Speeds up development
2οΈβ£ Installing Composer
β Installation on Windows
1οΈβ£ Download the Composer installer from getcomposer.org.
2οΈβ£ Run the installer and follow the instructions.
3οΈβ£ Open Command Prompt and verify installation:
composer -V
β Installation on macOS/Linux
Run the following command in the terminal:
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
Verify installation:
composer -V
You should see something like:
Composer version 2.x.x
3οΈβ£ Creating a New PHP Project with Composer
Letβs create a new project and manage dependencies using Composer.
1οΈβ£ Initialize a New Composer Project
Navigate to your project directory and run:
composer init
It will ask for details like:
- Package name (
zeroexp/dev-project
) - Description (
My first PHP project with Composer
) - Author (
Zero Dev <zero@zeroexp.dev>
) - Minimum stability (
stable
)
At the end, Composer generates a composer.json file.
4οΈβ£ Installing PHP Packages with Composer
π‘ Letβs install some useful packages using Composer!
Example 1: Installing a Logger (monolog/monolog)
composer require monolog/monolog
π₯ What happens?
β
Downloads the monolog/monolog
package from Packagist.
β
Adds it to composer.json
.
β
Creates a vendor/
folder with all dependencies.
Using the Installed Package in PHP
<?php
require "vendor/autoload.php"; // Automatically loads installed packages
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$log = new Logger("app");
$log->pushHandler(new StreamHandler("app.log", Logger::WARNING));
$log->warning("This is a warning!");
$log->error("This is an error!");
?>
π₯ Why use vendor/autoload.php
?
β
Automatically loads all installed packages without requiring files manually.
5οΈβ£ Updating and Removing Packages
Update Packages to Latest Version
composer update
Remove a Package
composer remove monolog/monolog
π₯ What happens?
β
Removes the package from composer.json
and vendor/
.
6οΈβ£ Using Composer Autoload for Your Own Classes
π‘ You can also autoload your own PHP classes!
1οΈβ£ Create a Folder for Your Code
mkdir src
2οΈβ£ Create a Sample Class in src/Utils/Helper.php
<?php
namespace App\Utils;
class Helper {
public static function greet($name) {
return "Hello, $name!";
}
}
?>
3οΈβ£ Configure Autoloading in composer.json
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
4οΈβ£ Run the Composer Dump-Autoload Command
composer dump-autoload
π₯ What happens?
β
Generates vendor/autoload.php
to automatically load classes.
5οΈβ£ Use Your Custom Class in index.php
<?php
require "vendor/autoload.php";
use App\Utils\Helper;
echo Helper::greet("Zero Dev");
?>
π₯ Boom! No more require
statements! π
7οΈβ£ Creating Your Own Composer Package
Want to share your code with the world? Create a public Composer package!
Step 1: Create a Package Directory
mkdir my-package && cd my-package
composer init
Step 2: Add Your Code (src/Calculator.php
)
<?php
namespace MyPackage;
class Calculator {
public function add($a, $b) {
return $a + $b;
}
}
?>
Step 3: Configure Autoloading in composer.json
"autoload": {
"psr-4": {
"MyPackage\\": "src/"
}
}
Step 4: Run composer dump-autoload
composer dump-autoload
Step 5: Test Your Package
<?php
require "vendor/autoload.php";
use MyPackage\Calculator;
$calc = new Calculator();
echo $calc->add(5, 10); // Output: 15
?>
π₯ Now your package is ready for Packagist! π
8οΈβ£ Publishing Your Package to Packagist
π‘ Want to make your package public?
1οΈβ£ Push your code to GitHub
git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/yourusername/my-package.git
git push -u origin main
2οΈβ£ Submit your package to Packagist
- Go to packagist.org
- Submit your GitHub repository link
- Run
composer require yourname/my-package
to install it! π
π― Mini Project: Build a JSON API with Composer Packages
Letβs build a simple JSON API using Composer.
1οΈβ£ Install Dependencies
composer require slim/slim "^4.0"
composer require monolog/monolog
2οΈβ£ Create index.php
<?php
require "vendor/autoload.php";
use Slim\Factory\AppFactory;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$app = AppFactory::create();
$log = new Logger("api");
$log->pushHandler(new StreamHandler("api.log", Logger::INFO));
$app->get('/hello/{name}', function ($request, $response, $args) use ($log) {
$log->info("Saying hello to " . $args['name']);
$response->getBody()->write(json_encode(["message" => "Hello, " . $args['name']]));
return $response->withHeader('Content-Type', 'application/json');
});
$app->run();
?>
3οΈβ£ Start the API Server
php -S localhost:8000
4οΈβ£ Test the API
Visit:
http://localhost:8000/hello/ZeroDev
π₯ You just built an API using Composer! π
π Final Thoughts
Now youβre a Composer pro!
β
Install and manage PHP packages
β
Use Composer for autoloading
β
Create and publish your own packages
β
Build real-world projects with Composer
π Next: PHP and APIs
Happy coding! ππ