DEV Community

loading...
Cover image for How to create a PHP package for Composer

How to create a PHP package for Composer

whoisryosuke profile image Ryosuke Originally published at whoisryosuke.com ・2 min read

Recently, I've recently been trying my best to upload as much code to open source repositories like Github and Codepen. It makes bootstrapping new projects much simpler when I can git clone a boilerplate from my Github, or copypasta a CSS/JS snippet from CodePen. But what happens when I need to include a library or module into several projects? Cloning and copy paste just don't cut it at that point.

Cut to NPM and Composer ( or really Packagist. NPM is a package manager for Javascript using Node, and Composer does the same for PHP.

Today we'll be looking into submitting a PHP "package" to Composer through the Packagist, so we can use composer require to install our package into any project!

The simple steps

It's as easy as 4 steps (minus the part where you code a reusable package in PHP) -- I was surprised too. Make sure before starting you've created a git repository in your project and committed your code. It's also required to have an account on Github and Packagist.

  1. Create a composer.json in your project:
{
    "name": "your-brand-name/your-project",
    "type": "library",
    "description": "Your package description goes here",
    "keywords": ["relevant","tags","go","here"],
    "homepage": "https://yourcompany.com",
    "license": "MIT",
    "authors": [
        {
            "name": "Jordi Boggiano",
            "email": "j.boggiano@seld.be",
            "homepage": "http://seld.be",
            "role": "Developer"
        }
    ],
}
Enter fullscreen mode Exit fullscreen mode
  1. Create a new repository on your Github and push your local git (init if you haven't) to the remote Github repo.

  2. Submit your package to the Packagist

  3. Done! Your package should be online and Packagist should provide a sample Composer require with your project name (e.g. composer require username/package-name)

You should be able to view your package live at: https://packagist.org/packages/your-username/package-name

So easy, there's no excuse

One of the biggest principles of programming I try to apply is DRY, don't repeat yourself. When your code is probably already hosted on Github, it's only a two step process to get your code in a place where it's version controlled.

Rather than relying on git clone or submodules, it's much more efficient for your codebase to use a composer dependency.

Take my code for a test run

The package I released on the Packagist is for developers working with the Metrc API to send and receive cannabis regulatory data. It's basically a wrapper for their API that uses a Guzzle client to interface with it (using your authoriation credentials).

If you're a Metrc developer, or just interested in using the code, you can add it to your projects using:

composer require kushy/metrc-php-sdk

Hope that helps,
Ryo

Discussion (3)

pic
Editor guide
Collapse
whoisryosuke profile image
Ryosuke Author

Using Composer allows you to leverage PSR-4 autoloading. You use it by adding an autoload property to the composer.json with your app or package's namespace.

Check out this guide on PHP namespaces for more info

Collapse
belhassen07 profile image
Belhassen Chelbi

I worked with a composer for a little project, and coming from npm for my javascript stack. it was quite a bad experience.
Do you think that composer is evolving with the amount needed?
great post though mate