Default

How to create and publish a PHP composer package

Composer is a dependency manager tool for PHP packages or libraries. The Composer uses its official repository packagist.org packages.

The Packagist is like a central Composer repository. It aggregates public PHP packages installable with Composer.

Composer packages are a great way of making your code available to everyone. It may be daunting to create your first package, but if you break it down into simple steps, it becomes much less scary!
Step 1. Create your composer file.
Before going forward, you should have the Composer installed. Just run the command:

composer init

Composer tool will begin the interactive Composer file creation. Firstly, you will need to define the package name.

Package name (<vendor>/<name>) [d4d/test-project]

A few things to note:

  1. The vendor is the author of the package.
  2. The name is the name of the package.

In our case, it will be d4d/test-project.
Next, the Composer will ask for the description of your package.
After that, the Composer will ask for the author, the minimal stability, package type, and License.
Fantastic, we’ve created our Composer file.

Now, we can adjust our composer.json file a little bit.

{
  "name": "d4d/test-project",
  "version": "0.1.0",
  "autoload": {
    "classmap": [
      "src/"
    ]
  },
  "require": {
    "php": ">=5.3"
  }
}

Now, let’s create our class inside of src.
Here is the example class I am using:

<?php

namespace D4d\TestProject;

class TestProjectClass
{

}

To generate the autoloader, just run the command

composer dump-autoload

This will generate an autoloader containing our class and helper file. We can now include our autoloader in our files.

If we make an index.php and add:

<?php

require_once 'vendor/autoload.php';

$class = new \D4d\TestProject\TestProjectClass();

We’ll see if our autoloader is working! From here, you can add all the code you want.

Step 2: Git repository.
Once your class(es) are ready, we must attach the code to a git repo. Once you have created and added your repository to your codebase, you’ll need a .gitignore in your project root containing vendor/. This will prevent your repository from being filled with Composer dependency gunk. Your repo is important since Packagist will use it to fetch your code. You’ll need to create a release for your main branch. You can version it as v0.1.0. This will prevent min-stability errors when we try to install it later!

Step 3: Submitting the package.
We will use Packagist to submit our package to the Composer repository. Head to Packagist and create your account. Once your account is created, click the “submit” button. You will be prompted to enter the URL of your git repository. Once submitted, your package will be available as a Composer package!

Now, you can install your package through Composer with the command

composer require d4d/test-project

Where d4d/test-project is the name of your package, which we inputted when we created the composer file.