How to Create and Publish a Composer Package for PHP

This step‑by‑step guide shows how to set up a Composer package directory, write the package code, configure autoloading, add tests, publish the package to Packagist, and integrate it locally into a Laravel project, complete with all necessary commands and code examples.

php Courses
php Courses
php Courses
How to Create and Publish a Composer Package for PHP

Step 1: Create Package Directory

In this step you create a directory to store your Composer package.

# Create a new directory for your package.
mkdir name-of-your-package
# Switch to that directory.
cd name-of-your-package

Initialize the Composer file with composer init and follow the prompts to set the package name, version, author, and description.

Package name

Package version

Package author

Package description

Step 2: Create Package Files

Create the source code for the package.

# Create the source code directory.
mkdir src
# Create a PHP file for the class.
touch src/HelloWorld.php

Edit src/HelloWorld.php and add the following class:

<?php

namespace YourVendor\HelloWorld;

class HelloWorld
{
    public function sayHello()
    {
        return 'Hello, World!';
    }
}

The class HelloWorld defines a sayHello() method that returns a simple greeting.

Step 3: Configure Autoloading

Add PSR‑4 autoloading information to composer.json so Composer can load classes from the src/ directory.

{
    "autoload": {
        "psr-4": {
            "YourVendor\\HelloWorld\\": "src/"
        }
    },
    "prefer-stable": true,
    "minimum-stability": "dev"
}

Run composer dump-autoload to generate the autoload.php file containing the package’s autoload information.

Step 4: Write Tests

Create a tests directory and add a PHPUnit test for the package.

mkdir tests
touch tests/HelloWorldTest.php

Edit tests/HelloWorldTest.php with the following content:

<?php

use YourVendor\HelloWorld\HelloWorld;
use PHPUnit\Framework\TestCase;

class HelloWorldTest extends TestCase
{
    public function testSayHello()
    {
        $helloWorld = new HelloWorld();
        $this->assertEquals('Hello, World!', $helloWorld->sayHello());
    }
}

Running the test should output OK (1 test, 1 assertion).

Step 5: Publish the Package

To make the package available to others, publish it on Packagist . Ensure you have a Packagist account, then submit the repository.

After publishing, the package can be required in other projects with:

composer require yourvendor/helloworld

Step 6: Local Testing in a Laravel Project

To test the package locally, add it as a path repository in your Laravel project's composer.json:

{
    "repositories": [
        {
            "type": "path",
            "url": "/path/to/HelloWorld"
        }
    ],
    "require": {
        "yourvendor/helloworld": "*"
    }
}

Replace /path/to/HelloWorld with the actual path, then run composer update to install the package.

Use the package in Laravel code:

<?php

use YourVendor\HelloWorld\HelloWorld;

$helloWorld = new HelloWorld();
echo $helloWorld->sayHello();

This basic example helps you get started; more advanced features such as configuration files, views, or migrations can be added as needed.

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.

PHPComposerLaravelPackagistPackage Development
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.