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.
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:
<code>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';</code>Step 3: Composer initialization (manual)
You can also create composer.json manually, for example:
<code>{
"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"}
}</code>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/" .
<code>"autoload": {
"psr-4": {"Lattice\\": "src/"}
}</code>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:
<code>namespace Lattice;
/**
* Class Lattice
*/
class Lattice {
public function index() {
echo "嘿嘿嘿";
}
}</code>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.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.