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.
Step 1: Create Package Directory
In this step you create a directory to store your Composer package.
<code># Create a new directory for your package.
mkdir name-of-your-package
# Switch to that directory.
cd name-of-your-package
</code>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.
<code># Create the source code directory.
mkdir src
# Create a PHP file for the class.
touch src/HelloWorld.php
</code>Edit src/HelloWorld.php and add the following class:
<code><?php
namespace YourVendor\HelloWorld;
class HelloWorld
{
public function sayHello()
{
return 'Hello, World!';
}
}
</code>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.
<code>{
"autoload": {
"psr-4": {
"YourVendor\\HelloWorld\\": "src/"
}
},
"prefer-stable": true,
"minimum-stability": "dev"
}
</code>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.
<code>mkdir tests
touch tests/HelloWorldTest.php
</code>Edit tests/HelloWorldTest.php with the following content:
<code><?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());
}
}
</code>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:
<code>composer require yourvendor/helloworld
</code>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 :
<code>{
"repositories": [
{
"type": "path",
"url": "/path/to/HelloWorld"
}
],
"require": {
"yourvendor/helloworld": "*"
}
}
</code>Replace /path/to/HelloWorld with the actual path, then run composer update to install the package.
Use the package in Laravel code:
<code><?php
use YourVendor\HelloWorld\HelloWorld;
$helloWorld = new HelloWorld();
echo $helloWorld->sayHello();
</code>This basic example helps you get started; more advanced features such as configuration files, views, or migrations can be added as needed.
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.