WeBRT Română

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": {}
}
  • name must be unique (vendor/package).
  • autoload defines 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

  1. Go to Packagist.org
  2. Create an account or log in
  3. Click "Submit" → enter the GitHub repository URL
  4. 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:


5️⃣ Use the package in projects

Once published:

composer require webrteu/my-package

Composer will automatically download the code from your repository.