How to Create and Publish a PHP Composer Package to GitHub and Packagist

This guide walks you through preparing a GitHub and Packagist account, creating a repository, initializing a Composer package (both automatically and manually), configuring PSR‑4 autoload, writing source code, and finally publishing the package by pushing to GitHub and submitting it to Packagist.

php Courses
php Courses
php Courses
How to Create and Publish a PHP Composer Package to GitHub and Packagist

Preparation

Ensure you have a GitHub account, a Packagist account, Git installed, and Composer installed on your machine.

Step 1: Create and clone a repository

Click the "+" button on GitHub, select "New repository", create the repo, then clone it locally using git clone <repo‑url>.

Step 2: Composer initialization (automatic)

Run composer init and follow the interactive prompts to generate a composer.json file. Example output:

composer init

Welcome to the Composer config generator

Package name (<vendor>/<name>) [z/lattice-php]: // your package name
Description []: // description
Author [Your Name <[email protected]>]: // author
Minimum Stability []: // e.g., stable
Package Type (e.g. library, project) []: // library
License []: MIT

Define your dependencies.
Would you like to define your dependencies (require) interactively [yes]? ...
Add PSR-4 autoload mapping? Maps namespace "Z\LatticePhp" to the entered relative path. [src/, n to skip]:

{
    "name": "z/lattice-php",
    "license": "MIT",
    "autoload": {
        "psr-4": {"Z\\LatticePhp\\": "src/"}
    },
    "authors": [{"name": "Chenilove","email": "[email protected]"}],
    "require": {}
}

Do you confirm generation [yes]? yes
Generating autoload files
Generated autoload files
PSR-4 autoloading configured. Use "namespace Z\LatticePhp;" in src/
Include the Composer autoloader with: require 'vendor/autoload.php';

Step 3: Composer initialization (manual)

You can also create composer.json manually, for example:

{
    "name": "zmxy/lattice",
    "description": "PHP lattice component. Tutorial: https://github.com/Chenilove/LatticePHP",
    "license": "MIT",
    "autoload": {"psr-4": {"Lattice\\": "src/"}},
    "authors": [{"name": "Chenilove","email": "[email protected]"}],
    "minimum-stability": "stable",
    "require": {"php": ">=7.1.0"}
}

Step 4: Autoload configuration

The autoload section defines the namespace‑to‑directory mapping. To add another namespace, edit the psr-4 object, e.g., "Lattice\\": "src/".

"autoload": {
    "psr-4": {"Lattice\\": "src/"}
}

After any change, refresh the autoloader with composer dump-autoload.

Step 5: Create source code

Create a src directory and add your PHP classes, for example:

namespace Lattice;

/**
 * Class Lattice
 */
class Lattice {
    public function index() {
        echo "嘿嘿嘿";
    }
}

Step 6: Push code to GitHub

Commit your changes with Git and push them to the remote GitHub repository.

Step 7: Submit to Packagist

Copy the GitHub repository URL (e.g., https://github.com/YunMengs/LatticePHP).

Open Packagist, click the "Submit" button.

Paste the repository URL into the "Repository URL" field and click "Check".

Release a new version on GitHub; Packagist will automatically update.

Each new release will trigger Packagist to refresh the package.

After completing these steps, your Composer package is publicly available and can be required by other projects.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendGitHubautoloadPackagistPSR-4
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.