Creating a Composer Package
This tutorial walks through the steps needed to build a package that can later be installed with Composer.
1️⃣ Create the repository on GitHub
- Create a new repository:
webrteu/my-package - It can be public or private (for public Packagist listing, it must be public)
2️⃣ PHP package structure
The package needs at least the following structure:
my-package/
├─ src/
│ └─ MyClass.php
├─ composer.json
└─ README.md
Example composer.json
{
"name": "webrteu/my-package",
"description": "My custom PHP package",
"type": "library",
"autoload": {
"psr-4": {
"Webrteu\\MyPackage\\": "src/"
}
},
"require": {}
}
namemust be unique (vendor/package).autoloaddefines the namespace and the folder that holds the code.
3️⃣ Commit and push to GitHub
git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin https://github.com/USERNAME/my-package.git
git push -u origin main
4️⃣ Publish on Packagist
- Go to Packagist.org
- Create an account or log in
- Click "Submit" → enter the GitHub repository URL
- Packagist will index the repository and automatically create the package
⚡ Recommendation: enable the GitHub Service Hook so Packagist automatically receives updates on every commit.
In GitHub -> Repository -> Settings -> WebHooks, add:
- Payload URL: https://packagist.org
- Secret: the value from https://packagist.org -> Profile -> Show Main API Token
5️⃣ Use the package in projects
Once published:
composer require webrteu/my-package
Composer will automatically download the code from your repository.