अगर आप PHP development करते हैं, तो आपने composer install, composer update या composer require जैसे commands जरूर देखे होंगे।
ये सभी commands Composer से संबंधित हैं।
Composer PHP का एक dependency management tool है। इसकी मदद से आप अपने PHP project में external libraries और packages install, update और manage कर सकते हैं।
उदाहरण के लिए, अगर आपको अपने PHP project में किसी library की जरूरत है, तो आपको उसकी files manually download करके project में रखने की जरूरत नहीं होती। Composer यह काम automatically कर सकता है।
इस guide में हम Composer को beginner-friendly Hinglish में practical examples के साथ सीखेंगे।
1. What is Composer?#
Composer PHP का dependency manager है।
Dependency का मतलब है कोई external library या package जिस पर आपका project depend करता है।
मान लीजिए आप एक PHP application बना रहे हैं और उसमें इन features की जरूरत है:
- Email भेजना
- PDF generate करना
- HTTP API requests करना
- Date और time manage करना
- Testing करना
इन सभी features के लिए आप existing PHP packages का उपयोग कर सकते हैं।
Composer इन packages को install और manage करने में मदद करता है।
Why use Composer?
- External PHP libraries आसानी से install करना।
- Package versions manage करना।
- Dependencies को automatically resolve करना।
- Autoloading setup करना।
- Team members के बीच consistent dependencies maintain करना।
- Project को दूसरे environment में setup करना आसान बनाना।
याद रखें: Composer PHP के लिए package और dependency management tool है। यह PHP framework नहीं है।
2. How Composer Works#
Composer मुख्य रूप से आपके project की dependency information के आधार पर काम करता है।
इसमें कुछ important files होती हैं:
| File / Folder | Purpose |
|---|---|
composer.json | Project की dependencies और configuration define करता है |
composer.lock | Exact installed dependency versions record करता है |
vendor/ | Installed packages और generated autoload files रखता है |
vendor/autoload.php | Composer का generated autoloader |
Simple Workflow
- Project में
composer.jsoncreate करें। - Required packages define या install करें।
- Composer dependencies resolve करता है।
- Packages
vendor/directory में install होते हैं। - Autoloading के माध्यम से classes use कर सकते हैं।
3. Install Composer#
Composer install करने से पहले आपके system में PHP installed होना चाहिए।
PHP version और installation check करने के लिए:
php -vComposer version check करें:
composer --versionअगर Composer installed नहीं है, तो official website से installation instructions follow करें।
Official Website: https://getcomposer.org/
macOS Installation
Homebrew available होने पर:
brew install composerInstallation verify करें:
composer --versionWindows Installation
Windows के लिए official Composer installer download करें:
Installer में PHP executable का path select करें और installation complete करें।
Linux Installation
Linux पर Composer install करने के लिए official download instructions follow करें।
Installation के बाद:
composer --versionImportant: Production environment में Composer installer और downloaded packages की authenticity verify करने के लिए official security instructions follow करें।
4. Create Your First Composer Project#
सबसे पहले एक नया project folder बनाएं।
mkdir composer-demo
cd composer-demoअब Composer initialize करें:
composer initComposer आपसे package name, description, author और dependencies जैसी information पूछ सकता है।
इसके बाद project में composer.json file create हो जाएगी।
आप manually भी composer.json create कर सकते हैं।
Basic composer.json
{
"name": "theeasymaster/composer-demo",
"description": "My first Composer project",
"type": "project",
"require": {}
}यह आपके project की basic Composer configuration है।
5. What is composer.json?#
composer.json Composer की main configuration file है।
इसमें project की dependencies, package information, autoloading configuration और scripts define किए जा सकते हैं।
Example
{
"name": "theeasymaster/demo",
"description": "Composer learning project",
"type": "project",
"require": {
"monolog/monolog": "^3.0"
}
}यहाँ:
name: Package या project का नाम।description: Project के बारे में जानकारी।type: Package का type।require: Production dependencies की list।monolog/monolog: Package का नाम।^3.0: Allowed version range।
Important: composer.json में dependency का version constraint define होता है। इसका मतलब यह जरूरी नहीं कि हर installation में बिल्कुल वही version install हो।
6. What is composer.lock?#
composer.lock file में Composer द्वारा resolve की गई exact dependency versions और संबंधित package information record होती है।
मान लीजिए आपके composer.json में है:
{
"require": {
"monolog/monolog": "^3.0"
}
}Composer compatible version resolve करके composer.lock में उसका exact version record करता है।
composer.json vs composer.lock
| composer.json | composer.lock |
|---|---|
| Dependency requirements define करता है | Resolved exact versions record करता है |
| Version constraints रखता है | Selected versions रखता है |
| Developer इसे update करता है | Composer commands इसे update कर सकती हैं |
| Project requirements बताता है | Consistent installation में मदद करता है |
Should You Commit composer.lock?
Application projects में सामान्यतः composer.lock को Git repository में commit करना चाहिए।
इससे team members और deployment environments में वही resolved dependency versions install किए जा सकते हैं।
Important: Libraries और reusable packages के लिए lock file का व्यवहार application projects से अलग हो सकता है। Library consumers अपनी dependency resolution के अनुसार versions प्राप्त करते हैं।
7. Install Dependencies#
अगर project में composer.json मौजूद है, तो dependencies install करने के लिए:
composer installयह command dependency requirements के अनुसार packages install करती है।
अगर composer.lock मौजूद है, तो Composer उसमें recorded versions install करने की कोशिश करता है।
When to use composer install?
- New project setup करते समय।
- Git से project clone करने के बाद।
- Deployment के समय।
vendor/directory missing होने पर।
Example:
git clone https://github.com/example/project.git
cd project
composer installयाद रखें: सामान्य project setup के लिए composer install सबसे common command है।
8. Install a Package Using Composer#
किसी package को install करने के लिए composer require command का उपयोग होता है।
Example:
composer require monolog/monologयह command:
- Package को resolve करती है।
- Package install करती है।
composer.jsonupdate करती है।composer.lockupdate करती है।- Autoload files generate या update करती है।
Example: Install a Specific Version
composer require monolog/monolog:^3.0यह compatible 3.x versions की अनुमति देता है, लेकिन exact selected version Composer resolve करता है।
Install a Development Dependency
कुछ packages केवल development और testing के लिए जरूरी होते हैं।
ऐसे packages को require-dev में add करें:
composer require --dev phpunit/phpunit9. Production vs Development Dependencies#
Composer में dependencies के लिए दो important sections हैं:
requirerequire-dev
Example
{
"require": {
"monolog/monolog": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "^11.0"
}
}| require | require-dev |
|---|---|
| Application runtime dependencies | Development dependencies |
| Production में जरूरी | सामान्यतः production में जरूरी नहीं |
| Production install में शामिल | --no-dev से exclude हो सकती हैं |
Production environment में:
composer install --no-devइससे development dependencies install नहीं होंगी।
10. Update Dependencies#
Dependencies update करने के लिए:
composer updateयह command composer.json में defined version constraints के अनुसार dependencies के नए compatible versions resolve करती है।
इसके बाद composer.lock update हो जाती है।
Update a Specific Package
composer update monolog/monologयह specific package और जरूरत पड़ने पर उससे संबंधित dependencies update कर सकता है।
Important Difference
| Command | Purpose |
|---|---|
composer install | Lock file के versions install करता है |
composer update | Dependencies को दोबारा resolve करके lock file update करता है |
composer require | New dependency add करके install करता है |
composer remove | Dependency remove करता है |
Best Practice: बिना जरूरत production project पर composer update run न करें। पहले changes review और test करें।
11. Remove a Package#
अगर किसी package की जरूरत नहीं है, तो उसे remove कर सकते हैं।
composer remove monolog/monologयह package को remove करने के साथ composer.json और composer.lock को भी update करता है।
Development dependency remove करने के लिए भी यही command काम करती है।
12. Check Installed Packages#
Project में installed packages देखने के लिए:
composer showSpecific package की information देखने के लिए:
composer show monolog/monologOutdated packages check करने के लिए:
composer outdatedDependencies की problems check करने के लिए:
composer diagnoseइन commands से project की dependency health समझने में मदद मिलती है।
13. What is the vendor Directory?#
जब Composer packages install करता है, तो वह सामान्यतः project में vendor/ directory create करता है।
Example structure:
composer-demo/
│
├── composer.json
├── composer.lock
│
├── vendor/
│ ├── autoload.php
│ ├── composer/
│ └── monolog/
│
└── index.phpvendor/ directory में installed packages और generated autoload files होती हैं।
Should You Commit vendor/?
सामान्यतः vendor/ directory को Git repository में commit नहीं किया जाता।
इसे .gitignore में add करें:
/vendor/इसके बजाय composer.json और application projects में composer.lock commit करें।
14. What is Autoloading?#
Autoloading का मतलब है classes को जरूरत पड़ने पर automatically load करना।
बिना autoloading के आपको कई files को manually include या require करना पड़ सकता है।
Composer का autoloader इस काम को आसान बनाता है।
Without Composer Autoloading
<?php
require_once "classes/User.php";
require_once "classes/Product.php";
require_once "classes/Order.php";
$user = new User();
?>जैसे-जैसे project बड़ा होता है, manual includes manage करना मुश्किल हो सकता है।
With Composer Autoloading
सबसे पहले Composer autoload file include करें:
<?php
require_once __DIR__ . "/vendor/autoload.php";इसके बाद configured classes automatically load हो सकती हैं।
Important: Composer autoloading तभी काम करेगी जब आपकी classes के लिए सही autoload configuration set हो।
15. PSR-4 Autoloading#
PSR-4 PHP में namespace के आधार पर classes automatically load करने का standard तरीका है।
Composer में PSR-4 configuration composer.json के autoload section में define होती है।
Project Structure
composer-demo/
│
├── composer.json
├── index.php
│
└── src/
└── User.phpStep 1: Create User.php
<?php
namespace App;
class User
{
public function getName(): string
{
return "Rahul";
}
}Step 2: Configure composer.json
{
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
}इसका मतलब है कि App\ namespace की classes src/ directory में खोजी जाएंगी।
Step 3: Generate Autoload Files
composer dump-autoloadयह command Composer की autoload files generate या update करती है।
Step 4: Use the Class
<?php
require_once __DIR__ . "/vendor/autoload.php";
use App\User;
$user = new User();
echo $user->getName();
?>Output:
RahulPSR-4 Mapping Example
| Namespace | Directory |
|---|---|
App\ | src/ |
App\Controllers\ | src/Controllers/ |
App\Models\ | src/Models/ |
ध्यान रखें कि namespace और directory structure सही match होना चाहिए। File और class naming में case consistency भी maintain करें।
16. What is dump-autoload?#
जब आप अपनी composer.json की autoload configuration change करते हैं, तो Composer को autoload files दोबारा generate करने की जरूरत हो सकती है।
इसके लिए:
composer dump-autoloadOptimized autoload files generate करने के लिए:
composer dump-autoload -oProduction deployment में optimized autoloading उपयोगी हो सकती है।
Difference:
composer install: Dependencies install करता है।composer dump-autoload: Autoload files regenerate करता है।
17. Composer Scripts#
Composer scripts की मदद से आप frequently used commands को composer.json में define कर सकते हैं।
Example
{
"scripts": {
"test": "phpunit",
"start": "php -S localhost:8000"
}
}अब commands run कर सकते हैं:
composer testcomposer startScripts development workflow को आसान बनाती हैं।
Note: Script तभी काम करेगी जब संबंधित executable उपलब्ध हो और उसका configuration सही हो।
18. Version Constraints in Composer#
Composer में package versions define करने के लिए version constraints का उपयोग होता है।
Common Version Constraints
| Constraint | Meaning |
|---|---|
1.2.3 | Exact version |
^1.2 | Compatible versions, जो 1.0.0 से 2.0.0 तक की सीमा में हों |
~1.2 | 1.2 series के compatible updates |
>=1.0 | 1.0 या उससे ऊपर |
* | कोई भी version |
^3.0 | Compatible 3.x versions |
Example
{
"require": {
"monolog/monolog": "^3.0"
}
}यहाँ ^3.0 का अर्थ है compatible 3.x releases की अनुमति।
Important: Version constraints का actual behavior package versioning और Composer rules पर निर्भर करता है। Production projects में broad constraints को समझकर use करें।
19. Platform Requirements#
Composer केवल packages की dependencies नहीं देखता, बल्कि PHP और extensions जैसी platform requirements भी check करता है।
उदाहरण के लिए, किसी package को PHP 8.2 या किसी specific extension की जरूरत हो सकती है।
Check करें:
composer check-platform-reqsPHP version:
php -vInstalled PHP extensions:
php -mIgnore Platform Requirements
Composer में यह option मौजूद है:
composer install --ignore-platform-reqsलेकिन इसे production solution की तरह use नहीं करना चाहिए।
यह missing PHP version या extension requirements को bypass कर सकता है, जबकि application runtime पर fail हो सकती है।
20. Composer Configuration Example#
एक practical PHP project की composer.json कुछ ऐसी दिख सकती है:
{
"name": "theeasymaster/my-app",
"description": "A PHP application",
"type": "project",
"require": {
"php": "^8.2",
"monolog/monolog": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "^11.0"
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"scripts": {
"test": "phpunit"
}
}इस configuration में:
- PHP 8.2 या compatible version required है।
- Monolog production dependency है।
- PHPUnit development dependency है।
App\namespacesrc/directory से map होता है।composer testtesting script run करता है।
Note: यह learning example है। Actual project में package versions और PHP requirements को अपने environment के अनुसार select करें।
21. Common Composer Errors and Solutions#
| Error / Problem | Possible Solution |
|---|---|
composer: command not found | Composer installation और PATH check करें |
| PHP version mismatch | Required PHP version install करें |
| Missing PHP extension | Required extension enable/install करें |
| Package not found | Package name और repository check करें |
| Autoload class not found | Namespace, file path और PSR-4 mapping check करें |
vendor/autoload.php missing | composer install run करें |
| Dependency conflict | Package constraints और dependency requirements check करें |
| Memory limit error | PHP CLI memory configuration और Composer troubleshooting guide check करें |
Useful Troubleshooting Commands
composer diagnosecomposer validatecomposer clear-cachecomposer dump-autoloadइन commands को problem के अनुसार use करें। हर error का solution cache clear करना नहीं होता।
22. Composer Best Practices#
- Application projects में
composer.lockGit में commit करें। vendor/directory को सामान्यतः Git में commit न करें।- Production dependencies और development dependencies अलग रखें।
- Package install करने से पहले उसकी documentation और maintenance status check करें।
- Unnecessary packages install न करें।
- Production deployment में
composer install --no-devuse करें, जब development dependencies की जरूरत न हो। - Package updates को test करने के बाद deploy करें।
- Autoloading के लिए PSR-4 standard prefer करें।
- Composer और PHP versions की compatibility check करें।
- Unknown packages को blindly trust न करें।
23. Composer Quick Reference#
| Command | Purpose |
|---|---|
composer --version | Composer version check |
composer init | New Composer configuration create |
composer install | Dependencies install |
composer update | Dependencies resolve और update |
composer require package/name | New package install |
composer require --dev package/name | Development dependency install |
composer remove package/name | Package remove |
composer show | Installed packages list |
composer outdated | Outdated packages check |
composer validate | composer.json और lock file validate |
composer diagnose | Common configuration problems check |
composer dump-autoload | Autoload files regenerate |
composer dump-autoload -o | Optimized autoload files generate |
composer install --no-dev | Development dependencies के बिना install |
composer check-platform-reqs | Platform requirements check |
composer clear-cache | Composer cache clear |
24. Composer Interview Questions#
Q1. What is Composer in PHP?
Composer PHP का dependency management tool है, जो external packages install और manage करता है।
Q2. What is composer.json?
यह project की dependencies और Composer configuration define करने वाली file है।
Q3. What is composer.lock?
यह resolved dependency versions record करती है ताकि project में consistent package versions install किए जा सकें।
Q4. What is the difference between composer install and composer update?
composer install lock file में recorded versions install करता है। composer update dependencies को फिर से resolve करके lock file update करता है।
Q5. What is the vendor directory?
यह directory Composer द्वारा installed packages और generated autoload files रखती है।
Q6. What is autoloading?
Autoloading classes को जरूरत पड़ने पर automatically load करने की सुविधा है।
Q7. What is PSR-4?
PSR-4 PHP में namespace और directory structure के आधार पर class autoloading का standard है।
Q8. What is composer require?
यह command project में नया package add और install करती है।
Q9. What is require-dev?
इसमें development और testing के लिए required dependencies define की जाती हैं।
Q10. What is composer dump-autoload?
यह Composer की autoload files regenerate करता है।
Q11. Why should we commit composer.lock?
Application project में इसे commit करने से team और deployment environments में resolved dependency versions consistent रहते हैं।
Q12. What is the difference between Composer and npm?
Composer PHP ecosystem का dependency manager है, जबकि npm JavaScript ecosystem में packages और project dependencies manage करता है।
25. Practice Exercises#
इन exercises को अपने local PHP environment में perform करें।
Beginner Level
composer initसे एक नया PHP project बनाएं।composer.jsonfile को समझें।- Monolog package install करें।
composer showसे installed packages देखें।- एक package remove करके देखें।
Intermediate Level
requireऔरrequire-devमें difference समझें।composer.lockgenerate करें और उसके contents देखें।- PSR-4 autoloading configure करें।
src/directory में दो PHP classes बनाएं और autoloading से use करें।composer dump-autoloadrun करके देखें।
Advanced Level
- एक छोटा PHP project बनाएं जिसमें Monolog और PSR-4 autoloading use हो।
- Composer scripts configure करें।
- Project को GitHub पर push करें और
vendor/को.gitignoreमें रखें। - दूसरी directory में project clone करके
composer installrun करें।
26. Summary#
Composer PHP projects में dependencies manage करने का एक essential tool है।
इस guide में आपने सीखा:
- Composer क्या है और क्यों जरूरी है।
- Composer installation।
composer.jsonऔरcomposer.lock।- Package install, update और remove करना।
vendor/directory।- Autoloading और PSR-4।
- Composer scripts।
- Version constraints।
- Platform requirements।
- Common errors और best practices।
अगर आप professional PHP developer बनना चाहते हैं, तो Composer और PSR-4 autoloading की अच्छी understanding जरूरी है।
27. What to Learn Next?#
Composer के बाद अगला topic है SQL (Structured Query Language)।
SQL की मदद से आप databases में data store, retrieve, update और delete करना सीखेंगे।
Recommended Learning Path:
- PHP Cheat Sheet
- PHP OOP Cheat Sheet
- Composer Cheat Sheet
- SQL Cheat Sheet
- MySQL Cheat Sheet
Official References#
- Composer – Official Documentation
- Composer – Basic Usage
- Composer – CLI Commands
- Composer – Autoloading
- PHP-FIG – PSR Standards
Practice Tip: केवल Composer commands याद न करें। एक छोटा PHP project बनाएं, उसमें package install करें, PSR-4 autoloading configure करें और फिर Git से clone करके composer install run करें। इससे पूरा workflow practically समझ आएगा।