Learn How to create and publish a composer php package

Srinath Reddy • January 1, 2021

php composer

Composer is a great tool to import php packages hosted on packagist.org. All the packages hosted on packagist are public packages and anyone can use them with in their package.

In this article, I am going to show you

First, We are going to create a simple hello world composer php package.

1. Create composer package

create a directory called hello-world-package.

mkdir hello-world-package && cd hello-world-package

Every composer package must have a composer.json file in the root directory. composer.json file holds information about a package like its name, dependencies, autoload classes, etc.

Create a new file called composer.json in the project root directory.

sudo nano composer.json

Copy and paste the following code.

{
    "name": "your-github-username/hello-world-package",
    "description": "A hello world composer private package.",
    "autoload": {
        "psr-4": {
            "YourGithubUserName\\HelloWorldPackage\\": "src/"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}

Replace your-github-username with your github profile name and save the file.

Full composer.json file schema can be found here

Now that we have our composer.json file all setup, We can start writing actual code.

Create a new directory called src in the project root.

mkdir src

Create a file called HelloWorld.php inside the src directory.

sudo nano HelloWorld.php

Copy and paste the below php code into the file and save the file.

<?php

namespace HelloWorldPackage;

class HelloWorld
{
    public static function sayHello()
    {
        echo "Hello World";
    }
}

Now our package is complete and ready to be distributed to the world. We first need to host this package on github before we start using it.

2. Setup Github Repository

In order to host packages on packagist.org and use them with composer, All the packages in github must be marked as public. I wrote a separate article on how to use composer with private repositories.

3. Sync package with github

Now that we have our package and github repository ready, We can push our package code to github. - Navigate to your project root directory in a terminal - Run git init to initialize your package as a github repository. - If you are using https for git, Run the following command

git remote add origin https://github.com/your-github-username/hello-world-package.git
git remote add origin [email protected]:your-github-username/hello-world-package.git
git add .
git commit -m "my first composer package"
git push --set-upstream origin master

4. Release package

Our package is finally on github. But before we can make use of our package, We need to release our version on github and provide a version label.

5. Host package on packagist.org

After successfully submitting your package to packagist, You can access it from the url https://packagist.org/packages/<your-username>/<package-name>

6. Import the package in a project

Now that our package is all setup on packagist.org, We can start importing and using it in our existing projects.

Import your package by running

composer require <your-username>/<your-package-name>

Add the composer autoload file at the beginning of script(If you are not already doing it)

<?php

require 'path-to-vendor-directory/autoload.php';

You can start calling the method in the package as below

<?php

require 'path-to-vendor-directory/autoload.php';


\YourGithubUserName\HelloWorldPackage\HelloWorld::sayHello();

Tips for creating composer packages

Did you enjoy reading the above article? share your thoughts with me on twitter. Also be sure to check out my recent posts.